I need a regular expression which matches:
http://example.com/foo
http://example.com/foo/
http://example.com/foo/bar
but not:
http://example.com/foobar
Using http://example.com/foo/?
matches the three types, but it matches /foobar
too that I don't want. What should I add to the regex to not match /foobar
?
Try this one:
^http://example\.com/foo(?:/.*)?$
Try something like this:
http://example.com/foo(?:\/|/(\w+)|)
In regex form:
/http:\/\/example.com\/foo(?:\/|\/(\w+)|)/
This will match example.com/foo or example.com/foo/bar or example.com/foo/
Some explaination:
(foo|bar)
matches foo or bar(?:)
a group with the ?:
in the begin will not been captured\/
will match a / at the end\/(\w+)
match a / with a word character who is repeated one or more times|)
will match nothing at the end of the string.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With