1

I am trying to extract an id number from urls in the form of

http://www.domain.com/some-slug-here/person/237570
http://www.domain.com/person/237570

either one of these urls could also have params on them

http://www.domain.com/some-slug-here/person/237570?q=some+search+string
http://www.domain.com/person/237570?q=some+search+string

I have tried the following expressions to capture the id value of '237570' from the above urls but each one kinda works but does work across all four url scenarios.

(?<=person\/)(.*)(?=\?)
(?<=person\/)(.*)(?=\?|\z)
(?<=person\/)(.*)(?=\??*)

what I am seeing happening is it is getting the 237570 but including the ? and characters that come after it in the url. how can I say stop capturing either when you hit a ?, /, or the end of the string?

1
  • thanks d3t0n4t0 and m.buettner - i should've included that the ids can be alphanumeric such as /person/35004902B839ABA2 I changed the (.*) to (\w{1,}) and it works great! Commented Oct 4, 2012 at 1:43

2 Answers 2

2

String:

http://www.domain.com/some-slug-here/person/1234?q=some+search+string
http://www.domain.com/person/3456?q=some+search+string
http://www.domain.com/some-slug-here/person/5678
http://www.domain.com/person/7890

Regexp:

person\/(\d{1,})

Output:

>>> regex.findall(string)
[u'1234', u'3456', u'5678', u'7890']
Sign up to request clarification or add additional context in comments.

Comments

1

Don't use .* to match the ID. . will match any character (except for line breaks, unless you use the DOTALL option). Just match a bunch of digits: (.*) --> (\d+)

3 Comments

..or the greedy, perhaps: (.*?)
yes, that should work, too. but I find it not really elegant to match a number with .*. One should keep the regex as constrained as possible to avoid unexpected special cases. Of course, you could still add the ungreedy ? to the digit-version, but it's not necessary any more.
Agreed. I was just thinking wider, ignoring the numeric requirement. Thanks for the correction btw. I don't know why I wrote greedy instead of ungreedy :)

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.