开发者

How to make Regexs only match on exact-matches?

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?


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/.


Append space at start and end of keyword you want to match with. For example you have a string "Hey! foobar I am foo bar." Now lets say you want to match foo. You can do this /\s+foo\s+/i this will return match for only foo and not foobar.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜