【问题标题】:Find words start with specific word in regex [duplicate]在正则表达式中查找以特定单词开头的单词[重复]
【发布时间】:2016-01-21 18:42:31
【问题描述】:

我正在尝试使用新关键字查找单词。下面是我的代码。

        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 关键字之后找到没有空格的单词。上面的代码搜索有空格和没有空格。

【问题讨论】:

    标签: regex c#-4.0


    【解决方案1】:

    你可以使用这个正则表达式:

    \bNew(\S.+?):
    

    匹配New 后跟一个非空格

    RegEx Demo

    否则:

    \bNew\B(.+?):
    

    匹配New,后跟非单词边界

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-06
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      • 2017-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多