0
http://www.foobar.com/foo/foo_bar.html?id=abc12A63768adv
http://www.foobar.com/foo/foo_bar.html?id=gsdfcc6tfsdfgg
http://www.foobar.com/foo/foo_bar.html?id={8765ABDV-8876-CR56-654A-ADD}

These are the url pattern for which I have to write a regex.

re.compile('http://www.foobar.com/foo/foo_bar.html?id=+[a-zA-Z0-9-{}]+$')

But this is not working for me.

1
  • 1
    For starters, escape the ?. Commented Mar 24, 2012 at 20:10

2 Answers 2

1

You must escape (by prepending \) the ? else it will be interpreted as an "optional" flag for the preceding part of the regex. Then, you should escape the dots, else they will match any character.

Then, you should move the - (dash) you are trying to match to the front of your [] list, so that the regex will be obviously including the dash and not using the dash as another range.

Finally, you may wish to put ^ on the front so that you always match from the beginning, similar to how you used $ on the end.

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

Comments

1

here is your pattern :

re.compile('http://www.foobar.com/foo/foo_bar.html\?id=[a-zA-Z0-9{}\-]+$')
  1. ? is a regex reserved keyword, it needs to be escaped
  2. the + in "id=+" is wrong, as it will match "id=" and "id======"
  3. between [] - is a regex reserved keyword, it needs to be escaped
  4. as john said, you should add ^ for matching on a whole string

Comments

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.