Reputation: 11
I am trying to match on any incoming requests that don't have a trailing slash. I also want to capture the URL path, to use in a capture group. Here is what I've gotten so far.
Incoming URL: http://www.example.com/path/to/
Regex: http:\/\/www.example.com\/(.*)[.\/]
Here's a link to the regex tester results
This captures what I want, but it always matches on all URLs - not the URLs that don't contain a trailing slash.
The regex processor is PERL based.
What do I need to change?
Upvotes: 0
Views: 684
Reputation: 8833
You want a not-capture group at the end and match line start/end like this
^http:\/\/www.example.com\/(.*)[^\/]$
(replace "match period or /" [.\/]
with "not /" [^\/]
, and surround with ^$
)
(You where so close!)
Add s?
to make https?
to allow an optional s if you don't care which protocol is being used.
Upvotes: 1
Reputation: 627607
To match URLs that do not end with /
, use a negative lookbehind with an $
anchor like below:
http:\/\/www.example.com\/(.*)$(?<!\/)
^^^^^^^^
$
will assert the position at the end of the string, and the lookbehind will fail the match if there is a slash. See this regex demo.
Another way is with a (?!.*\/$)
lookahead:
http:\/\/www.example.com\/(?!.*\/$)(.*)
^^^^^^^^^
See another regex demo.
The (?!.*\/$)
negative lookahead will fail the match after finding out that there is /
at the end of the string ($
). Note that .*
(matches any 0+ chars) is necessary to get to the end of the string, you cannot just use (?!\/$)
.
Upvotes: 1