Take the 2-minute tour ×

I am having some URLs in this format. Some URLs contain &abc=4 and some not.


here xxxxxxxxxxxxxxxxxxxxx is string

I want to match URLs which have xxxxxxxxxxxxxxxxx only and not &abc=4(meaning I want to get these type of URLs, only xxxxxxxxxxxxxx, xxxxxxxxxxxxxx, xxx)

I know how to write a regular expression which matches the entire url. For example: /x.*abc=4/

But how do I write a regular expression that matches only xxxxxxxxxx and not &abc=4?

share|improve this question
first check for a match with xxxx, then check for the absence of abc=4 –  knittl Mar 3 '11 at 11:46
probably you also don't want ?abc=4 –  Benoit Mar 3 '11 at 11:57
probably you also also don't want ;abc=4 –  tadmc Mar 3 '11 at 13:01
add comment

3 Answers

I would use negative look-ahead assertion (Look ahead what is not allowed to follow my pattern)


This pattern will match any string that does not end with &abc=4

you can verify it online here: http://www.rubular.com/

share|improve this answer
add comment

Use negative lookbehind assertion. The form is:


(this will also exclude ?abc=4).

share|improve this answer
add comment

Assuming your URLs are on each line, you can use:


This basically will match anything up to the the first instance of &.

As @Benoit said, you can do this using a zero width expression to negate the capture of the query string, but you would be after a positive lookahead, and not a negative lookbehind, syntax example below:


As you can see though, this would complicate the expression a touch.

Hope this helps.

share|improve this answer
will it not work /x.*(?!&abc=4)/ –  Mark Gill Mar 3 '11 at 12:10
That's a negative lookahead, so what your asking for is everything up to the point that it would not be followed by the &=444 point. A positive lookahead allows you to say everything up the point that it is followed by this. –  Pooli Mar 3 '11 at 12:21
add comment

Your Answer


