2016-01-21 96 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

謝謝 –

相關問題