Reputation: 4583
I don't know what to add to my regular expression to make it only match the 'exact' patterns.
#users/(.+?)/#
with this string users/john/someweirdstuff/
will be a match.
I am getting 'john
' and not 'john/someweirdstuff
' (which is a good step in the right direction), but I don't want it to match at all. Because the string (which are supposed to be URLs) aren't the same.
So basically, what do I add behind my reg exp to make it say "there should nothing after the last slash"
Upvotes: 0
Views: 164
Reputation: 175028
You should use the $
character that matches the end of the string.
You should also avoid matching "any character (.
)" and you should match "any character which is not a slash ([^/]
). As shown in the example).
Also note I omitted the question mark (?
), the question mark in this instance changes the character matching from greedy (match as much as possible) to lazy (match as few as possible).
Use it like so:
#users/([^/]+)/$#
Upvotes: 1
Reputation: 242103
If you want to say "there should be nothing after the last slash", say "the string ends after the last slash", i.e. #users/(.+)/$#
. Ommitting the question mark should also work, making the plus sign "greedy", i.e. matching as much as it can.
Upvotes: 0