i need a regular expression to validate the field. It is a field of phone numbers, the number consists of 9 characters, numbers will be separated by commas.
我需要一个正则表达式来验证该字段。它是一个电话号码字段,数字由9个字符组成,数字将用逗号分隔。
Example:
123456789,123456789,123456789
123456789
123456789,123456789
I have written:
我已经写了:
~^(([0-9]{9,9},)+|([0-9]{9,9})+)$~i
but validating passes only numbers with commas.
但验证只传递带逗号的数字。
Example:
123456789,123456789,
123456789,
Can you help me with this?
你能帮我解决这个问题吗?
3 个解决方案
#1
2
You need to set a *
quantifier to the first 9-digit group:
您需要将*量词设置为第一个9位数组:
~^(?:[0-9]{9},)*[0-9]{9}$~
^
See the regex demo. No need in ~i
case insensitive modifier, there are no letters in the pattern. Also, {9,9}
= {9}
.
请参阅正则表达式演示。不需要~i不区分大小写的修饰符,模式中没有字母。此外,{9,9} = {9}。
The same pattern can be written as
相同的模式可以写成
~^[0-9]{9}(?:,[0-9]{9})*$~
See another demo.
看另一个演示。
Details:
-
^
- start of string -
(?:[0-9]{9},)*
- 0+ sequences of:-
[0-9]{9}
- 9 digits -
,
- comma
[0-9] {9} - 9位数
, - 逗号
-
-
[0-9]{9}
- 9 digits -
$
- end of string (may be replaced with\z
to match the very end of string).
^ - 字符串的开头
(?:[0-9] {9},)* - 0+序列:[0-9] {9} - 9位数, - 逗号
[0-9] {9} - 9位数
$ - 字符串结尾(可以用\ z替换以匹配字符串的结尾)。
EDIT: Since you only want to match comma-separated 9-digit chunks that only contain unique digits, it is possible to write a regex for that, although it is not recommended to use it. Best is to use your programming language means for that.
编辑:由于您只想匹配仅包含唯一数字的逗号分隔的9位数块,因此可以为其编写正则表达式,但不建议使用它。最好是使用您的编程语言手段。
A regex will look like (verbose version):
正则表达式看起来像(详细版本):
^ # start of string
( # Group 1 start
(\d) # Digit 1 captured into Group 2
(?!\2)(\d) # Digit 2 not equal to the first one
(?!\2|\3)(\d) # etc.
(?!\2|\3|\4)(\d)
(?!\2|\3|\4|\5)(\d)
(?!\2|\3|\4|\5|\6)(\d)
(?!\2|\3|\4|\5|\6|\7)(\d)
(?!\2|\3|\4|\5|\6|\7|\8)(\d)
(?!\2|\3|\4|\5|\6|\7|\8|\9)(\d)
)
(?:,(?1))* # 0+ sequences of , and the Group 1 pattern
$ # End of string
See the regex demo. A one-liner:
请参阅正则表达式演示。单行:
^((\d)(?!\2)(\d)(?!\2|\3)(\d)(?!\2|\3|\4)(\d)(?!\2|\3|\4|\5)(\d)(?!\2|\3|\4|\5|\6)(\d)(?!\2|\3|\4|\5|\6|\7)(\d)(?!\2|\3|\4|\5|\6|\7|\8)(\d)(?!\2|\3|\4|\5|\6|\7|\8|\9)(\d))(?:,(?1))*$
In PHP, you may just use
在PHP中,您可以使用
if (count( array_unique( str_split( $s))) == strlen($s)) {
echo "Unique";
} else {
echo "Not unique";
}
#2
0
I don't think that your regex has to be very complicated:
我不认为你的正则表达式必须非常复杂:
^\d{9}(,\d{9})*,?
- It will match at least one number block consisting of 9 digits.
- Other 9 digit blocks might follow, if they are separated by a comma.
- The whole expression can end with a comma.
它将匹配至少一个由9位数字组成的数字块。
如果用逗号分隔,则可能会跟随其他9位数的块。
整个表达式可以用逗号结尾。
#3
0
Just move that plus(+) to the end of the bigger group
只需将加号(+)移动到更大的组的末尾即可
^(([0-9]{9,9},)|([0-9]{9,9}))+$
#1
2
You need to set a *
quantifier to the first 9-digit group:
您需要将*量词设置为第一个9位数组:
~^(?:[0-9]{9},)*[0-9]{9}$~
^
See the regex demo. No need in ~i
case insensitive modifier, there are no letters in the pattern. Also, {9,9}
= {9}
.
请参阅正则表达式演示。不需要~i不区分大小写的修饰符,模式中没有字母。此外,{9,9} = {9}。
The same pattern can be written as
相同的模式可以写成
~^[0-9]{9}(?:,[0-9]{9})*$~
See another demo.
看另一个演示。
Details:
-
^
- start of string -
(?:[0-9]{9},)*
- 0+ sequences of:-
[0-9]{9}
- 9 digits -
,
- comma
[0-9] {9} - 9位数
, - 逗号
-
-
[0-9]{9}
- 9 digits -
$
- end of string (may be replaced with\z
to match the very end of string).
^ - 字符串的开头
(?:[0-9] {9},)* - 0+序列:[0-9] {9} - 9位数, - 逗号
[0-9] {9} - 9位数
$ - 字符串结尾(可以用\ z替换以匹配字符串的结尾)。
EDIT: Since you only want to match comma-separated 9-digit chunks that only contain unique digits, it is possible to write a regex for that, although it is not recommended to use it. Best is to use your programming language means for that.
编辑:由于您只想匹配仅包含唯一数字的逗号分隔的9位数块,因此可以为其编写正则表达式,但不建议使用它。最好是使用您的编程语言手段。
A regex will look like (verbose version):
正则表达式看起来像(详细版本):
^ # start of string
( # Group 1 start
(\d) # Digit 1 captured into Group 2
(?!\2)(\d) # Digit 2 not equal to the first one
(?!\2|\3)(\d) # etc.
(?!\2|\3|\4)(\d)
(?!\2|\3|\4|\5)(\d)
(?!\2|\3|\4|\5|\6)(\d)
(?!\2|\3|\4|\5|\6|\7)(\d)
(?!\2|\3|\4|\5|\6|\7|\8)(\d)
(?!\2|\3|\4|\5|\6|\7|\8|\9)(\d)
)
(?:,(?1))* # 0+ sequences of , and the Group 1 pattern
$ # End of string
See the regex demo. A one-liner:
请参阅正则表达式演示。单行:
^((\d)(?!\2)(\d)(?!\2|\3)(\d)(?!\2|\3|\4)(\d)(?!\2|\3|\4|\5)(\d)(?!\2|\3|\4|\5|\6)(\d)(?!\2|\3|\4|\5|\6|\7)(\d)(?!\2|\3|\4|\5|\6|\7|\8)(\d)(?!\2|\3|\4|\5|\6|\7|\8|\9)(\d))(?:,(?1))*$
In PHP, you may just use
在PHP中,您可以使用
if (count( array_unique( str_split( $s))) == strlen($s)) {
echo "Unique";
} else {
echo "Not unique";
}
#2
0
I don't think that your regex has to be very complicated:
我不认为你的正则表达式必须非常复杂:
^\d{9}(,\d{9})*,?
- It will match at least one number block consisting of 9 digits.
- Other 9 digit blocks might follow, if they are separated by a comma.
- The whole expression can end with a comma.
它将匹配至少一个由9位数字组成的数字块。
如果用逗号分隔,则可能会跟随其他9位数的块。
整个表达式可以用逗号结尾。
#3
0
Just move that plus(+) to the end of the bigger group
只需将加号(+)移动到更大的组的末尾即可
^(([0-9]{9,9},)|([0-9]{9,9}))+$