2016-01-21 99 views
1

我正在尝试使用New关键字找到单词。以下是我的代码。查找单词开始于正则表达式中的特定单词

 string contents = " holla holla testing is for NewFinancial History:\"xyz\" dsd NewFinancial History:\"abc\" New Investment History:\"abc\" dsds "; 

     var keys = Regex.Matches(contents, @"New(.+?):", RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace).OfType<Match>().Select(m => m.Groups[0].Value.Trim().Replace(":", "")).Distinct().ToArray(); 

在上面的代码中,它同时搜索NewFinancial History:\“xyz \”和New Investment History:\“abc \”。 它应该只能找到NewFinancial History:\“xyz \”而不是New Investment History:\“abc \”。 我想在New关键字之后找到没有空格的单词。上面的代码使用和不使用空格来搜索。

+0

(?<= New)(\ S +)。* ?: – Aferrercrafter

回答

1

你可以使用这个表达式:

\bNew(\S.+?): 

匹配New后跟一个非空

RegEx Demo

要不然:

\bNew\B(.+?): 

匹配之后New非字边界

+1

谢谢 –

相关问题