【发布时间】:2019-01-11 10:00:59
【问题描述】:
我尝试使用带有否定后视的模式来获取不以 "un" 开头的单词。这是代码:
using Regexp = System.Text.RegularExpressions.Regex;
using RegexpOptions = System.Text.RegularExpressions.RegexOptions;
string quote = "Underground; round; unstable; unique; queue";
Regexp negativeViewBackward = new Regexp(@"(?<!un)\w+\b", RegexpOptions.IgnoreCase);
MatchCollection finds = negativeViewBackward.Matches(quote);
Console.WriteLine(String.Join(", ", finds));
它总是返回完整的单词集,但应该只返回round, queue。
【问题讨论】:
-
如果单词分隔符总是
;你甚至不必使用正则表达式 -String.Split结合 linq 就可以了 -
注意有
Regex类,而不是Regexp。 -
将
Regex别名为Regexp有什么意义?还有RegexpOptions?
标签: c# regex regex-negation regex-lookarounds