【问题标题】:Regex to extract digits followed by specific word正则表达式提取数字后跟特定单词
【发布时间】:2016-10-13 11:56:39
【问题描述】:

使用正则表达式,我想提取后跟特定单词的数字。

位数不是有限的。

示例输入:

My address is 1234@abc.com and you can send SMS to me.

预期结果。

1234

本例中具体单词为@abc.com,需要提取该单词后面的数字。

【问题讨论】:

    标签: c# .net regex


    【解决方案1】:

    使用正则表达式组:on MSDN。

    在 C# 中,试试这个:

    string pattern = @"(\d+)@abc\.com";
    string input = "My address is 15464684@abc.com and you can send SMS to me";
    Match match = Regex.Match(input, pattern);
    
    // Get the first named group.
    Group group1 = match.Groups[1];
    Console.WriteLine("Group 1 value: {0}", group1.Success ? group1.Value : "Empty");
    

    【讨论】:

      【解决方案2】:

      您需要匹配 1234@abc.com 并使用分组来提取数字:

      (\d+)\@abc.com
      

      【讨论】:

        【解决方案3】:

        .* (\d+)@abc\.com .* 应该可以工作。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-08-02
          • 1970-01-01
          相关资源
          最近更新 更多