我需要一个正则表达式来匹配一个值,其中每个字符可以是0到9之间的数字或空格.该值必须包含11位数字.
例如,它应匹配格式为"012 345 678 90"或"01234567890"的值.
有人可以帮我这个吗?
Iai*_*ser 17
为了其他澳大利亚开发商的利益,未来可能会发现这一点:
^(\d *?){11}$
匹配11个数字,每个数字后面有零个或多个空格.
编辑:
正如@ElliottFrisch所提到的,ABN还有一个适当验证的数学公式.使用正则表达式来正确验证ABN是非常困难的(或不可能的) - 尽管上述正则表达式至少匹配11位数字与间距.如果您正在进行实际验证,那么在这种情况下,正则表达式可能不适合您.
进一步阅读:
https://abr.business.gov.au/Help/AbnFormat
这是一个PHP实现:
http://www.clearwater.com.au/code
从上面的页面复制代码,以防有一天它变得不可用:
// ValidateABN
// Checks ABN for validity using the published
// ABN checksum algorithm.
//
// Returns: true if the ABN is valid, false otherwise.
// Source: http://www.clearwater.com.au/code
// Author: Guy Carpenter
// License: The author claims no rights to this code.
// Use it as you wish.
function ValidateABN($abn)
{
$weights = array(10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19);
// strip anything other than digits
$abn = preg_replace("/[^\d]/","",$abn);
// check length is 11 digits
if (strlen($abn)==11) {
// apply ato check method
$sum = 0;
foreach ($weights as $position=>$weight) {
$digit = $abn[$position] - ($position ? 0 : 1);
$sum += $weight * $digit;
}
return ($sum % 89)==0;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
还有一个我在这里找到的javascript:
http://www.mathgen.ch/codes/abn.html
以下公式可用于验证您分配的 ABN 或验证发给您经营的企业的 ABN。
要验证 ABN:
从第一个(左侧)数字中减去 1,得到一个新的 11 位数字 将这个新数字中的每个数字乘以其加权因子 将所得 11 个乘积相加。
将总数除以 89,记下余数。
如果余数为零,则该数字有效。
数字位置
1 2 3 4 5 6 7 8 9 10 11
重量
10 1 3 5 7 9 11 13 15 17 19
例如,检查 ABN 53 004 085 616 的有效性
5 3 0 0 4 0 8 5 6 1 6
第一个(左边)数字减去 1 得到新数字 4 3 0 0 4 0 8 5 6 1 6
应用加权因子 10 1 3 5 7 9 11 13 15 17 19
(4x10)+(3x1)+(0x3)+(0x5)+(4x7)+(0x9)+(8x11)+(5x13)+(6x15)+(1x17)+(6x19) 40+3+0+0 +28+0+88+65+90+17+114
445/89 = 5 余数 0
余数为零,因此该数字有效。
这是一个 C# 验证:
public static bool ValidateABN(string abn)
{
bool isValid = false;
int[] weight = { 10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
int weightedSum = 0;
//ABN must not contain spaces, comma's or hypens
abn = StripNonDigitData(abn);
//ABN must be 11 digits long
if (!string.IsNullOrEmpty(abn) & Regex.IsMatch(abn, "^\\d{11}$"))
{
//Rules: 1,2,3
for (int i = 0; i <= weight.Length - 1; i++)
{
weightedSum += (int.Parse(abn[i].ToString()) - ((i == 0 ? 1 : 0))) * weight[i];
}
//Rules: 4,5
return ((weightedSum % 89) == 0);
}
return isValid;
}
public static string StripNonDigitData(string input)
{
StringBuilder output = new StringBuilder("");
foreach (char c in input)
{
if (char.IsDigit(c))
{
output.Append(c);
}
}
return output.ToString();
}
Run Code Online (Sandbox Code Playgroud)
以及 VB.Net 验证:
Public Shared Function ValidateABN(ByVal abn As String) As Boolean
Dim isValid As Boolean = False
Dim weight() As Integer = {10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19}
Dim weightedSum As Integer = 0
'ABN must not contain spaces, comma's or hypens
abn = StripNonDigitData(abn)
'ABN must be 11 digits long
If Not String.IsNullOrEmpty(abn) And Regex.IsMatch(abn, "^\d{11}$") Then
'Rules: 1,2,3
For i As Integer = 0 To weight.Length - 1
weightedSum += (Integer.Parse(abn(i).ToString()) - (IIf(i = 0, 1, 0))) * weight(i)
Next
'Rules: 4,5
Return ((weightedSum Mod 89) = 0)
End If
Return isValid
End Function
Public Shared Function StripNonDigitData(ByVal input As String) As String
Dim output As New StringBuilder("")
For Each c As Char In input
If Char.IsDigit(c) Then
output.Append(c)
End If
Next
Return output.ToString
End Function
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7859 次 |
| 最近记录: |