0

Here is my input string

2014.10.30: b
l
ah bl
a
h  
2014.10.30: bfoo  
lah  
b  
l  
ah   
     2014.10.30: bart1  
     lah  
     baloon  
     l  
     ah  
2014.10.30: carb  
lah  
b  
l  
ah  
     2014.10.30: farb2  
     lah  
     baloon  
     l  
     ah  
2014.10.30: carb  
lah  
b  
l  
ah  
2014.10.30: bom  
lah  
baloob  
l  
ah  
etc

I would like to match the following (between the 2014 which has the word baloon inside them):

2014.10.30: bart1  
lah  
baloon  
l  
ah  

and

2014.10.30: farb2  
lah  
baloon  
l  
ah  

This is what I have,

/(2014)(.*?baloon.*)(?:2014)/gsmi  

and this is matching from the beginning of the first occurrence of 2014. And its returning only one match with bottom to top. regex101 here

Anand Rockzz
  • 6,072
  • 5
  • 64
  • 71
  • Different programming languages have different flavors of regex. Most of them support "getNextMatch". For example: http://stackoverflow.com/a/520845/1057429 – Nir Alfasi Nov 01 '14 at 02:57

1 Answers1

1

Use negative and positive lookahead assertion.

2014(?:(?!2014).)*?baloon.*?(?=\n2014)

DEMO

OR

2014(?:(?!2014).)*?baloon(?:(?!2014).)*?(?=\n2014)

(?:(?!2014).)*? would match any character but not of 2014. This asserts that there isn't a string 2014 present in-between the starting 2014 and baloon

DEMO

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274