18

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?

4

2 回答 2

36

用于^匹配字符串的开头和字符串$的结尾。

例如:^/$匹配/但不匹配/foo。和^/匹配/foo但不是foo/

于 2011-01-03T01:02:56.323 回答
-3

在要匹配的关键字的开头和结尾处附加空格。例如你有一个字符串"Hey! foobar I am foo bar."现在假设你想匹配 foo。您可以这样做,/\s+foo\s+/i这将只返回 foo 而不是 foobar 的匹配项。

于 2011-11-07T08:59:14.463 回答