mike
mike

Reputation: 5

Pattern to find all the data between two words

I am using the below pattern to find all the data between two words, placed in multiple lines.

/START(.+?)END/

But this doesn't seems to work as expected. Can someone help me out with a exact pattern.

Thanks.

Upvotes: 0

Views: 877

Answers (3)

Amarghosh
Amarghosh

Reputation: 59451

Use the s flag : with this flag, dot (.) will match new line characters too.

 /start(.+?)end/s

Upvotes: 2

ghostdog74
ghostdog74

Reputation: 342323

assuming you can use Python for example.

>>> s="""
... START
...   blah1 blah2
...   more blah  
...   and even more blah
... END blah3 blah4 blah5 START blah 6
... text here here                    
... blah7 END ......                  
... START end END                     
... """                            
>>> for item in s.split("END"):
...     if "START" in item:
...        print item [ item.find("START")+len("START") : ]
...

  blah1 blah2
  more blah
  and even more blah

 blah 6
text here here
blah7
 end

Split your string on "END", go through each splitted items, check for START,and do a substring. Use the split method of your preferred language.

Upvotes: 0

kennytm
kennytm

Reputation: 523214

Do you mean you want to match things like

START
  blah blah blah
  more blah
  and even more blah
END

? Since . does not match newlines by default, your regex won't work. You need to supply the single-line (also known as "dot-all") /s flag to make it match the newlines.

/START(.+?)END/s

Upvotes: 1

Related Questions