Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我遇到了正则表达式的问题...我必须匹配共享点 URL ..我需要匹配“最短”
就像是:
http://aaaaaa/sites/aaaa/aaaaaa/ m = Regex.Match(URL, ".+/sites/.+/");
m.Value 等于整个字符串...
我怎样才能使它匹配
http://aaaaaaa/sites/aaaa/
没有别的了??
非常感谢!
.+是贪婪的,所以它会在停止之前匹配尽可能多的字符。将其更改为.+?,比赛将尽快结束:
.+
.+?
m = Regex.Match(URL, ".+/sites/.+?/");
尝试使正则表达式匹配所有内容,/而不是简单的所有内容。这是通过使用字符类 atom 的非形式来完成的[^]。
/
[^]
m = Regex.Match(URL, ".+/sites/[^/]+/");