Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortest match in regex from end

Given an input string fooxxxxxxfooxxxboo I am trying to write a regex that matches fooxxxboo i.e. starting from the second foo till the last boo.

I tried the following

foo.*?boo matches the complete string fooxxxxxxfooxxxboo

foo.*boo also matches the complete string fooxxxxxxfooxxxboo

I read this Greedy vs. Reluctant vs. Possessive Quantifiers and I understand their difference, but I am trying to match the shortest string from the end which matches the regex i.e. something like the regex to be evaluated from back. Is there any way I can match only the last portion?

like image 309
Srinath Avatar asked Dec 10 '14 08:12

Srinath


Video Answer


2 Answers

Use negative lookahead assertion.

foo(?:(?!foo).)*?boo

DEMO

(?:(?!foo).)*? - Non-greedy match of any character but not of foo zero or more times. That is, before matching each character, it would check that the character is not the letter f followed by two o's. If yes, then only the corresponding character will be matched.

Why the regex foo.*?boo matches the complete string fooxxxxxxfooxxxboo?

Because the first foo in your regex matches both the foo strings and the following .*? will do a non-greedy match upto the string boo, so we got two matches fooxxxxxxfooxxxboo and fooxxxboo. Because the second match present within the first match, regex engine displays only the first.

like image 175
Avinash Raj Avatar answered Nov 03 '22 00:11

Avinash Raj


.*(foo.*?boo)

Try this. Grab the capture i.e $1 or \1.

See demo.

https://regex101.com/r/nL5yL3/9

like image 35
vks Avatar answered Nov 03 '22 02:11

vks