Reputation: 6122
My URLs look like this:
www.example.com/historical+locations/london
and
www.example.com/special/london
I want to see if the URL contains a certain format. Below expression works for the occurence of special
, but the +
sign in historical locations seems to mess up the matching on that string.
I tried the follow expressions, none of them match...I can't seem to match on a string containing a +
character
Dim urlMatch As Match = Regex.Match("/historical+locations/london",
"(^/special|^/\bhistorical+locations\b)/[a-zA-Z]",
RegexOptions.IgnoreCase)
Dim urlMatch As Match = Regex.Match("/historical+locations/london",
"(^/special|^/historical+locations)/[a-zA-Z]",
RegexOptions.IgnoreCase)
Dim urlMatch As Match = Regex.Match("/historical+locations/london",
"(^/special|^/historical+locations$)/[a-zA-Z]",
RegexOptions.IgnoreCase)
Upvotes: 1
Views: 46
Reputation: 18763
You need to escape the +
as it is a regex character meaning 1 or more [greedy]
to escape the regex character and make it a static character add a \
before the +
like so:
\+
so escaping the /
as well you get (also combining common regex that will speed up your search):
Dim urlMatch As Match = Regex.Match("/historical+locations/london",
"^\/(special|historical\+locations)\/[a-zA-Z]+",
RegexOptions.IgnoreCase)
^\/(special|historical\+locations)\/[a-zA-Z]+
Upvotes: 5
Reputation: 707
You must to escape the plus sign on regex \+
Dim urlMatch As Match = Regex.Match("/historical+locations/london",
"(^/special|^/\bhistorical\+locations\b)/[a-zA-Z]",
RegexOptions.IgnoreCase)
Upvotes: 3