linvi
linvi

Reputation: 115

Regex Or and Greedy

I am trying to perform this regex but I am not sure how to write the or condition because of the greedy behavior of regex.

some_text=(?<value>.*)&|some_text=(?<value>.*)$

The above regex is exactly what I want but I would like to write a better one that would look like the following:

some_text=(?<value>.*)(?:&|$)

But because of greediness, this one is not stopping the value when it encounters the first & instead it goes back to the end of the string.

Thanks for the help

Upvotes: 1

Views: 83

Answers (1)

anubhava
anubhava

Reputation: 785276

You can use just this negation regex:

some_text=(?<value>[^&]*)

[^&]* will match 0 or more of any character that is not &

Upvotes: 1

Related Questions