Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex negative lookahead

I need to modify this regex

href=\"(.*)\"

which matches this...

href="./pothole_locator_map.aspx?lang=en-gb&lat=53.153977&lng=-3.533306"

To NOT match this...

href="./pothole_locator_map.aspx?lang=en-gb&lat=53.153977&lng=-3.533306&returnurl=AbandonedVehicles.aspx"

Tried this, but with no luck

href=\"(.*)\"(?!&returnurl=AbandonedVehicles.aspx)

Any help would be much appreciated.

Thanks, Al.

like image 737
general exception Avatar asked Apr 15 '10 14:04

general exception


People also ask

What is a negative lookahead in regex?

The negative lookahead construct is the pair of parentheses, with the opening parenthesis followed by a question mark and an exclamation point. Inside the lookahead, we have the trivial regex u. Positive lookahead works just the same. q(?=u) matches a q that is followed by a u, without making the u part of the match.

How do you use negative look ahead?

Negative lookahead That's a number \d+ , NOT followed by € . For that, a negative lookahead can be applied. The syntax is: X(?! Y) , it means "search X , but only if not followed by Y ".

What is positive and negative lookahead in regex?

Positive lookahead: (?= «pattern») matches if pattern matches what comes after the current location in the input string. Negative lookahead: (?! «pattern») matches if pattern does not match what comes after the current location in the input string.

Can I use regex lookahead?

Lookahead assertions are part of JavaScript's original regular expression support and are thus supported in all browsers.


2 Answers

Lookaheads should be placed before the string is consumed by matching, i.e.

href=\"(?!.*&returnurl=AbandonedVehicles\.aspx)(.*)\"
like image 167
kennytm Avatar answered Oct 18 '22 15:10

kennytm


href="(?!.*returnurl=AbandonedVehicles\.aspx)(.*)"
like image 41
SilentGhost Avatar answered Oct 18 '22 15:10

SilentGhost