Geltrude
Geltrude

Reputation: 1095

Regular expression to find text between strings WITHOUT a predefined string in the middle

I've a so written text:

11 bla gulp bla 22
11 bla bla bla 2211 bla 
ble
bli 22

I need a regex to find all the text between all the couples "11" and "22" BUT that DON'T contain "gulp".

If I search (?s)11.*?22 using TextCrawler, I find all the three strings:

bla gulp bla
bla bla bla
bla ble bli

Wrong! I'd like to obtain only:

bla bla bla
bla ble bli

because "bla gulp bla" contains "gulp", and I don't want it!

Any idea? :-)

Upvotes: 1

Views: 1301

Answers (1)

Code Jockey
Code Jockey

Reputation: 6721

use a negative lookahead assertion:

11(?!.*?gulp.*?)(.*?)22

word boundaries might be a good idea in the middle (surrounding gulp), because it would allow to distinguish between gulp and gulping, gulped or ungulp(?):

11(?!.*?\bgulp\b.*?)(.*?)22

but putting them around everything:

\b11\b(?!.*?\bgulp\b.*?)(.*?)\b22\b

would exclude your other two results - not what you want.

Upvotes: 1

Related Questions