【问题标题】:Retrieve numeric values which are not with alphanumeric with dot using regular expressions使用正则表达式检索不带有带点的字母数字的数值
【发布时间】:2012-08-23 08:00:28
【问题描述】:

嗨,有人可以帮我从字符串中提取单独的数字吗..假设我的字符串如下

AB12(100).HJI(20).D76R(222)

我需要输出如下:

  100.20.222(I need to get the digits which are in brackets appended with dot)

【问题讨论】:

    标签: c# regex


    【解决方案1】:

    好的。这很简单:

    var value = @"AB12(100).HJI(20).D76R(222)"; 
    MatchCollection matches = Regex.Matches(value, @"(?<=\()(\d+)(?=\))");  
    var result = String.Join(matches, ".")
    

    【讨论】:

      【解决方案2】:

      另一个即兴创作:

      string input = "AB12(100).HJI(20).D76R(222)";
      string output = string.Join(".", Regex.Matches(input, @"\((?<value>\d{1,})\)").OfType<Match>().Select(m => m.Groups["value"].Value));
      

      【讨论】:

        【解决方案3】:
        var value = @"AB12(100).HJI(20).D76R(222)"; 
        var matches = Regex.Matches(value, @"(?<=\()(\d+)(?=\))");
        var result = String.Join(".", matches.OfType<Match>().Select(x => x.Value).ToArray());
        

        【讨论】:

          【解决方案4】:

          你也可以用this替换正则表达式:

          /[A-Za-z0-9]+\((\d+)\)/$1/g
          

          C# 代码为:

          string input = "AB12(100).HJI(20).D76R(222)";
          string pattern = @"[A-Za-z0-9]+\((\d+)\)";
          string result = System.Text.RegularExpressions.Regex.Replace(input, pattern, "$1");
          

          【讨论】:

            【解决方案5】:

            只是另一个版本

             string text = "AB12(100).HJI(20).D76R(222)";
             MatchCollection match = Regex.Matches(text, @"\(\d+\)"); //(100)(20)(222)
             string[] digits = new string[match.Count];
            
             for(int i=0; i<digits.Length;i++)
             {
                 digits[i] = match[i].Value.Trim(new char[]{'(',')'});
             }
             string output = String.Join(".", digits); //100.20.222
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2012-08-23
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-12-07
              • 1970-01-01
              • 2018-10-29
              • 1970-01-01
              相关资源
              最近更新 更多