【问题标题】:Regex extract 16 digit of contract Number正则表达式提取合同号的 16 位数字
【发布时间】:2017-12-24 13:37:52
【问题描述】:

我们的客户通常会向我们发送一些包含他们的合同号的消息。我们的合同号格式是从 1000 开始的 16 位数字字符串(例如:1000117030010745)。

现在我们的工具可以识别合同号,这些合同号使用正则表达式很好地放置,以找到彼此相邻的 16 位数字。

但问题是,有时它们在合同编号中间包含一些特殊字符,如点(.)、空格等,并且这些字符的数量不是恒定的。 例如:10001170.5102.1428 或 1000 11706 0056941。

是否有任何正则表达式可以匹配这些数字?

谢谢!

编辑 1:有可能将公民 ID 放在合同号旁边,用空格隔开。还有一些客户在合同号中同时添加“特殊字符”

例如:1000.1170。 1000.3828

【问题讨论】:

  • 删除任何属于这些“特殊字符”之一的内容,然后验证
  • @horcrux:到目前为止,您的答案是最好的,但是空格和点可能会彼此相邻添加,例如:1000.1170。 1000.3828。有什么建议吗?
  • 使用*{0,2} 代替?。抱歉,你写过正则表达式吗?

标签: c# regex


【解决方案1】:

这里有一个非常相似的问题: Regex to remove all special characters from string?

添加对命名空间的引用

using System.Text.RegularExpressions;

然后在你的身体里

// Example
string startNumber = "100.23 455 * 4332";

// Replace anything but numbers 0-9 with nothing
string formattedNumber = Regex.Replace(startNumber, [^0-9], "" ); 

// formattedNumber = 100234554332

【讨论】:

    【解决方案2】:

    您可以使用此行获取合同编号值

    var string='AASHGHSABCD1000. 65 .893 4652345EFGHIJKLMNASASOP'
    var res = string.replace(/[^a-zA-Z 0-9]+/g,'').replace(/\s/g, '').substr(string.indexOf("1000"), 16);
    alert(res)
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    【讨论】:

      【解决方案3】:

      使用[^\d] 删除所有非数字字符,并使用^$ 验证16 位输入(开始和结束)

      using System;
      using System.Text.RegularExpressions;
      
      class MainClass {
      
        public static void Main (string[] args) {
          // valid 16 digit
          string citizenID = "1000.1170. 1000.3826";
          // not valid more than 16 digit
          //string citizenID = "1000.1170. 1000.3826789";
          citizenID = Regex.Replace(citizenID, @"[^\d]", ""); 
          Match match = Regex.Match(citizenID, @"^\d{16}$");
          if(match.Success)
            Console.WriteLine ("valid citizenID: " + citizenID);
          else
            Console.WriteLine ("not valid citizenID");
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2018-11-14
        • 2020-06-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多