s4nji
s4nji

Reputation: 455

Limiting a greedy regular expression

Example string:

?token=a12b3c4d5e&time=1417111248&message=Lorem ipsum dolor sit amet.&mode=reply&bbcode=1&topic=123456789


Tried capturing the message with (?:\?|&)message=(.+)&, however it matches everything until the last & character:

Lorem ipsum dolor sit amet.&mode=reply&bbcode=1

How do I stop the capturing group when it encounters & for the first time?


Expected result:

Lorem ipsum dolor sit amet.

Upvotes: 0

Views: 41

Answers (1)

nu11p01n73R
nu11p01n73R

Reputation: 26667

You can use a very simple regex as

message=[^&]+

Example : http://regex101.com/r/rJ5qK2/1

  • [^&]+ matches anything other than a & one or more time

OR

You can also use a non greedy .+? as

message=(.+?)&

Example : http://regex101.com/r/rJ5qK2/2

Upvotes: 3

Related Questions