-1

I cannot figure out how to match only on groups that contain a certain word ('test' for example below). It is a big text file and the groups start with a line 'Group x' and include text with an empty line separation to the next group. I think I need to use lookaheads and lookbehinds but don't know how. I can use vb.net for this but trying to test out different expressions in the regex testers and can't get anywhere.

Group 1
adfdf
dd   test  ddfdf
dfdfadf

Group 2
ddfadfa

Group 3
add  test 
adfdff

Group 4
adfdf

Expected 2 matches:

Group 1
adfdf
dd   test  ddfdf
dfdfadf

Group 3
add  test 
adfdff
cyberdog
  • 11
  • 4
  • Not sure how to reword the question. I was specific in how the text file looks and what matches I expected. I got an answer below that works. thanks – cyberdog Aug 18 '16 at 18:07

1 Answers1

0

Start your pattern with ^Group \d+$ and end with (?:^$|\Z). In the middle match test but not preceeded by an empty line $(?:.(?!^$) (see Regular expression to match a line that doesn't contain a word? for details on how the latter works). Don't forget the m and s modifiers:

^Group \d+$(?:.(?!^$))*?test.*?(?:^$|\Z)

Demo: https://regex101.com/r/kM9qB3/2

Community
  • 1
  • 1
Dmitry Egorov
  • 9,542
  • 3
  • 22
  • 40
  • You're a genius! This works and I need to study this on how it works. I thought I needed some kind of lookarounds for this. I checked the web and couldn't find anything related to this puzzle. Maybe someone asked before, not sure. I am still learning regex and there seems to be different solution to the same puzzle. I am not sure if there are any others but it works. Thanks – cyberdog Aug 18 '16 at 17:08
  • I just noticed that if the word test is in the last Group section, then it doesn't match on it. It matches all the other groups correctly. – cyberdog Aug 18 '16 at 17:55
  • Update the answer to fix the last group issue. The closing pattern should be "either empty line or end of the whole text" - `(?:^$|\Z)` – Dmitry Egorov Aug 18 '16 at 19:13
  • In fact it contains a lookaround, namely negative lookahead, as a part of more complex "no empty line after newline but and before 'test'" - `$(?:.(?!^$))`. It is explained in details in the question I referred to in my post. – Dmitry Egorov Aug 18 '16 at 19:16
  • Cool thanks. Yes I have been looking at this and saw the lookaround after I wrote. It is going to take me awhile to figure out this regex stuff. – cyberdog Aug 18 '16 at 20:36