0

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

xxxxxxxxxxxxxxxxxxxxxxxxxxx&abc=4
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&abc=4
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx

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?

3
  • 1
    first check for a match with xxxx, then check for the absence of abc=4 Commented Mar 3, 2011 at 11:46
  • probably you also don't want ?abc=4 Commented Mar 3, 2011 at 11:57
  • probably you also also don't want ;abc=4 Commented Mar 3, 2011 at 13:01

3 Answers 3

1

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

^(?!.*&abc=4$).*$

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

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

Sign up to request clarification or add additional context in comments.

Comments

0

Use negative lookbehind assertion. The form is:

(?<![&?]abc=4)

(this will also exclude ?abc=4).

Comments

0

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:

(?=(&[^=]+?\d+)+)

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

Hope this helps.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.