Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex: text between first occurrence of two patterns

Tags:

regex

/url?q=http://it.wikipedia.org/wiki/Spider-Man_(film)&sa=U&ei=iavVUKuFGsrNswbz74GQBA&ved=0CBYQFjAA&usg=AFQjCNEth5YspFPWp6CInyAfknlEvVgIfA

I need to get just

http://it.wikipedia.org/wiki/Spider-Man_(film)

I tried with \?q=(.*)& but it consider last occurrence of &, so I get

http://it.wikipedia.org/wiki/Spider-Man_(film)&sa=U&ei=iavVUKuFGsrNswbz74GQBA&ved=0CBYQFjAA

http://rubular.com/r/yBiGIMQTUV

like image 315
sparkle Avatar asked Dec 07 '22 10:12

sparkle


1 Answers

You need to use reluctant matching to match till the first &. With greedy matching (i.e. using * instead of *?), your pattern will match as long string as possible so as to satisfy the complete pattern.

So use this: -

\?q=(.*?)&

Or you can also use character class with negated & which matches every character except &: -

\?q=([^&]*)

Note that, if you don't want your (.*?) to match empty string, then you should use + quantifier. It matches 1 or more occurrence.

like image 169
Rohit Jain Avatar answered Dec 30 '22 18:12

Rohit Jain