Ok, so basically I have some bit of code that matches URLs by regexs. It then will call some function based on which regex the URL matches against. I never want for more than one function to be called for a URL and I want the regex matches to have to be "exact"
For instance, with the simple URL /
I use a simple regex /
which will match /
but it will also match things like /foo
and /foo/bar
.
How can I prevent this partial matching behavior in C#/.Net?
But if you wish to match an exact word the more elegant way is to use '\b'. In this case following pattern will match the exact phrase'123456'.
Match any specific character in a setUse square brackets [] to match any characters in a set. Use \w to match any single alphanumeric character: 0-9 , a-z , A-Z , and _ (underscore). Use \d to match any single digit. Use \s to match any single whitespace character.
Example is: string pattern = @"(Band) (? <Band>[A-Za-z ]+) (? <City>@[A-Za-z ]+) (?
This operator is similar to the match-zero-or-more operator except that it repeats the preceding regular expression at least once; see section The Match-zero-or-more Operator ( * ), for what it operates on, how some syntax bits affect it, and how Regex backtracks to match it.
Use ^
for matching the start of a string and $
for the end of a string.
For example: ^/$
matches /
but not /foo
. And ^/
matches /foo
but not foo/
.
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