[英]Regular Expression for number and dash
Currently I write the regex like this: /^([\\d+]*-)+([\\d]*-)+([\\d])*$/
目前我写这样的正则表达式: /^([\\d+]*-)+([\\d]*-)+([\\d])*$/
I want the result follow this pattern 00-123-456-789
or 00123456789
(dash between number group or no dash at all) 我希望结果遵循这种模式00-123-456-789
或00123456789
(在数字组之间划线或根本没有破折号)
00-123--457-789
不是00-123--457-789
-00-123-456-789-
或-00-123-456-789-
-00123456789-
或-00123456789-
00-123-456-789-
或00-123-456-789-
How can I modify the regex to matches the pattern above? 如何修改正则表达式以匹配上面的模式?
If your question needs the specific segment lengths you specify in your examples, you can use this: 如果您的问题需要在示例中指定的特定段长度,则可以使用:
/^\d{2}-?\d{3}-?\d{3}-?\d{3}$/
This will accept 00-123-456-789
, but will allow for any dashes to be missing. 这将接受00-123-456-789
,但允许缺少任何破折号。 If you want to allow only for all dashes or no dashes, then you could use this: 如果您只想允许所有破折号或没有破折号,那么您可以使用:
/^\d{2}-\d{3}-\d{3}-\d{3}$|^\d{11}$/
which will accept only 00-123-456-789
or 00123456789
, but not allow only some dashes to be missing. 00-123-456-789
接受00-123-456-789
或00123456789
,但不允许只丢失一些破折号。
Or, if you meant that you could have any number of digits and any number of single dashes between them, then you could use this: 或者,如果您的意思是它们之间可以有任意数量的数字和任意数量的单个破折号,那么您可以使用:
/^\d+(-\d+)*$/
尝试这样的事情
/^(\d+-?)+\d+$/
If I understand you correctly, you wish your regex to stand for strings which consist of a number group followed (optionally) by additional number groups with a -
separator. 如果我理解正确的话,你希望你的正则表达式代表一个字符串,该字符串由一个数字组组成,后跟(可选)带有-
分隔符的附加数字组。
\d+ # represents a number group
(-\d+)* # represents 0 or more additional number groups beginning with "-"
So, together with the necessary beginning and end of line assertions, together we have: 因此,连同必要的开头和结尾断言,我们一起:
^\d+(-\d+)*$
The following answer will be used to match 以下答案将用于匹配
00-123-456-789 or 00 123 456 789 00-123-456-789或00 123 456 789
/^[\\d]{2}[-\\s]?[\\d]{3}[-\\s]?[\\d]{3}[-\\s]?[\\d]{3}$/ / ^ [\\ d] {2} [ - \\ s]的[\\ d] {3}?[ - \\ s]的[\\ d] {3}?[ - \\ S] [\\ d] {3} $ /
If you want only to match hyphen(-) or without hyphen(-) 如果你只想匹配连字符( - )或没有连字符( - )
Then it will do 然后它会做
/^[\\d]{2}[-]?[\\d]{3}[-]?[\\d]{3}[-]?[\\d]{3}$/ / ^ [\\ d] {2} [ - ] [\\ d] {3}?[ - ] [\\ d] {3}?[ - ] [\\ d] {3} $ /
如果您只想接受## - ### - ### - ###或###########,那么您需要的是:
/^(([\d+]{2}\-)([\d]{3}\-){2}([\d]{3})|[\d]{11})$/
@godwin: since I can't add a comment to your post and no one else seems to have responded, let me address at least one issue. @godwin:因为我无法在你的帖子中添加评论而其他人似乎没有回复,所以让我至少解决一个问题。 At the beginning you have 一开始你有
[\d+]{2}\-
\\d+
will match one or more digits and you're asking for it to be repeated twice, so the whole segment will match 123456789-
: \\d+
将匹配一个或多个数字,并且您要求重复两次,因此整个段将匹配123456789-
:
[\\d+]
= 12345678
(greedy matching) [\\d+]
= 12345678
(贪婪匹配) [\\d+]
= 9
[\\d+]
= 9
\\-
= -
\\-
= -
You also probably didn't need to escape the dash because it's not inside a group block "[]"
. 你也可能不需要逃避破折号,因为它不在组块"[]"
。
From what have you provided us, this regex ^-?(\\d+-?)+$
matches this 从你提供给我们的是什么,这个正则表达式^-?(\\d+-?)+$
匹配这个
-00-123-456-789-
-00123456789-
00-123-456-789-
00123456789
but doesn't match this: 但不符合这个:
00-123--457-789
Breakdown: 分解:
^-?
) 字符串可以用短划线开头( ^-?
) \\d+-?
) 它有一些数字后跟一个可选的短划线( \\d+-?
) (\\d+-?)+
) 最后一组可以重复一次或多次( (\\d+-?)+
) $
) 字符串结尾( $
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.