我们的客户通常会向我们发送一些信息,其中包括合同号。我们的合约编号格式是从1000开始的16位字符串(例如:1000117030010745)。
现在我们的工具可以识别合约编号,这些合约编号使用正则表达式可以找到相邻的16位数字。
但问题是,有时它们包括一些特殊字符,如点(。),空格等......在合约编号的中间,这些字符的数量不是恒定的。 例如:10001170.5102.1428或1000 11706 0056941。
有没有可以匹配这些数字的正则表达式?
谢谢!
编辑1:公民身份证号码可能会放在合约号码旁边,以空格分隔。并且一些客户将“特殊字符”添加到合同编号
ex:1000.1170。 1000.3828
答案 0 :(得分: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
答案 1 :(得分:0)
您可以使用此行
获取合约编号值
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>
答案 2 :(得分:0)
删除[^\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");
}
}