Least Possible Search / nongreedy regex

At first I thought this answer would solve my problem, but it doesn't.

I have a string url like this one:

http://www.someurl.com/some-text-1-0-1-0-some-other-text.htm#id_76

      

I would like to extract some-other-text

, so basically, I have the following regex:

/0-(.*)\.htm/

      

Unfortunately this matches 1-0-some-other-text

because the regex is greedy. I can't do this without using it .*?

, it doesn't change anything as you can see here .

I also tried with a modifier U

but it didn't help.

Why isn't the "nongreedy" prompt working?

+3


source to share


2 answers


If you need to get the closest possible match, you can use a moderate greedy token .

0-((?:(?!0-).)*)\.htm

      

Watch the demo

the lazy version of your regex doesn't work because the regex engine parses the string from left to right. It always has the leftmost position and checks if it can match. So, in your case, he found the first one 0-

and was happy with it. Laziness extends to the extreme right position. In your case, there is 1 possible extreme position, so a lazy match cannot help you achieve your expected results.



You can also use

0-((?!.*?0-).*)\.htm

      

It will work if you have separate rows to retrieve values.

+3


source


Do you want to exclude 1-0

? If so, you can use a non-capturing group:

(?:1-0-)+(.*?)\.htm

      



Demo

0


source







All Articles