I have a URL, and I’m trying to match it to a regular expression to pull out some groups. The problem I’m having is that the URL can either end or continue with a ‘/’ and more URL text. I’d like to match URLs like this:
- http://server/xyz/2008-10-08-4
- http://server/xyz/2008-10-08-4/
- http://server/xyz/2008-10-08-4/123/more
But not match something like this:
So, I thought my best bet was something like this:
/(.+)/(\d{4}-\d{2}-\d{2})-(\d+)[/$]
where the character class at the end contained either the ‘/’ or the end-of-line. The character class doesn’t seem to be happy with the ‘$’ in there though. How can I best discriminate between these URLs while still pulling back the correct groups?
1st Capturing Group
(.+).+matches any character (except for line terminators)+Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)2nd Capturing Group
(\d{4}-\d{2}-\d{2})\d{4}matches a digit (equal to[0-9]){4}Quantifier — Matches exactly 4 times-matches the character-literally (case sensitive)\d{2}matches a digit (equal to[0-9]){2}Quantifier — Matches exactly 2 times-matches the character-literally (case sensitive)\d{2}matches a digit (equal to[0-9]){2}Quantifier — Matches exactly 2 times-matches the character-literally (case sensitive)3rd Capturing Group
(\d+)\d+matches a digit (equal to[0-9])+Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)4th Capturing Group
(.*)??Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy).*matches any character (except for line terminators)*Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)$asserts position at the end of the string