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

1
  • Different programming languages have different flavors of regex. Most of them support "getNextMatch". For example: stackoverflow.com/a/520845/1057429 Commented Nov 1, 2014 at 2:57

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.