仅限数字和连字符的正则表达式 [英] Regex for digits and hyphen only

查看:483
本文介绍了仅限数字和连字符的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试理解正则表达式,对于长度为10的数字,我可以简单地执行

  / ^ [0-9] {10} $ / 

仅限连字符我可以做

  / ^ [ - ] $ / 

将两个使用组表达式组合将导致

  / ^([0-9] {10})|([ - ] )$ / 

此表达式无法正常工作,它会以某种方式匹配部分字符串而不是如果字符串无效,则完全不匹配。





如何使正则表达式只接受 - 或10位?

解决方案

它可以正常工作完全按照你的方式结合你的两个正则表达式。换句话说,只需使用交替/管道运算符来组合

  / ^ [0-9] {10} $ / 

  / ^ [ - ] $ / 

直接进入

  / ^ [0-9] {10} $ | ^ [ - ] $ / 
↑↑↑↑↑↑↑↑↑↑ ↑↑↑↑↑↑您的原始注册,与|结合使用

这可以表示为



< img src =https://www.debuggex.com/i/LgJ5zGXHmkoikWaF.pngalt =正则表达式可视化>



这样就可以了。正如其他人所指出的那样,你不需要在字符类中指定连字符,所以

  / ^ [0- 9] {10} $ | ^  -  $ / 
↑简化[ - ]只需 -

现在,我们注意到两个选项中的每一个在开头都有 ^ ,最后有一个 $ 。这有点重复,它也使得立即看到regexp总是从头到尾匹配的东西变得更加困难。因此,我们可以重写这一点,如其他答案中所解释的那样,从两个子中取出 ^ $ 正则表达式,并使用分组运算符组合其内容()

  / ^([0-9] {10} |  - )$ / 
↑↑↑↑↑↑↑↑↑↑↑↑↑GROUP REGEXP内容与PARENS,与ANCHORS外面的

相应的可视化是





这也可以正常工作,但你可以使用 \ 而不是 [0-9] ,所以最终,最简单的版本是:

  / ^(\d {10} |  - )$ / 
↑↑USE \d FOR DIGITS

这可视化为





如果由于某种原因您不想捕获该组,请使用(?:,如

  / ^(?:\\\ {10} |  - )$ / 
↑↑不要捕获集团

现在可视化显示未捕获组:





顺便说一句,在你的原来尝试结合两个正则表达式,我注意到你把它们括起来,如

  / ^([0-9] {10 })|([ - ])$ / 
↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑你参与了SUB-REGEXPS

但实际上这不是必需的,因为管道(或)运算符的替换已经具有低优先级(实际上它具有任何正则表达式运算符的最低优先级) ; 低优先级意味着它只适用于之后两边的东西都已处理完毕,所以你在这里写的内容与



/ ^ [0-9] {10} | [ - ] $ /



<然而,从其他答案中提到的原因仍然不起作用,从其可视化中可以清楚地看出:




I am trying to understand regex, for digits of length 10 I can simply do

/^[0-9]{10}$/

for hyphen only I can do

/^[-]$/

combining the two using group expression will result in

/^([0-9]{10})|([-])$/

This expression does not work as intended, it somehow will match part of the string instead of not match at all if the string is invalid.

How do I make the regex expression that accepts only "-" or 10 digits?

解决方案

It would have worked fine to combine your two regexps exactly as you had them. In other words, just use the alternation/pipe operator to combine

/^[0-9]{10}$/

and

/^[-]$/

as is, directly into

/^[0-9]{10}$|^[-]$/
 ↑↑↑↑↑↑↑↑↑↑↑ ↑↑↑↑↑     YOUR ORIGINAL REGEXPS, COMBINED AS IS WITH |

This can be represented as

and that would have worked fine. As others have pointed out, you don't need to specify the hyphen in a character class, so

/^[0-9]{10}$|^-$/
              ↑        SIMPLIFY [-] TO JUST -

Now, we notice that each of the two alternatives has a ^ at the beginning and a $ at the end. That is a bit duplicative, and it also makes it little harder to see immediately that the regexp is always matching things from beginning to end. Therefore, we can rewrite this, as explained in other answers, by taking the ^ and $ out of both sub-regexps, and combine their contents using the grouping operator ():

/^([0-9]{10}|-)$/
  ↑↑↑↑↑↑↑↑↑↑↑↑↑        GROUP REGEXP CONTENTS WITH PARENS, WITH ANCHORS OUTSIDE

The corresponding visualization is

That would also work fine, but you could use \d instead of [0-9], so the final, simplest version is:

/^(\d{10}|-)$/
   ↑↑                  USE \d FOR DIGITS

and this visualizes as

If for some reason you don't want to "capture" the group, use (?:, as in

/^(?:\d{10}|-)$/
   ↑↑                  DON'T CAPTURE THE GROUP

and the visualization now shows that group is not captured:

By the way, in your original attempt to combine the two regexps, I noticed that you parenthesized them as in

/^([0-9]{10})|([-])$/
  ↑↑↑↑↑↑↑↑↑↑↑ ↑↑↑↑↑    YOU PARENTHESIZED THE SUB-REGEXPS

But actually this is not necessary, because the pipe (alternation, of "or") operator has low precedence already (actually it has the lowest precedence of any regexp operator); "low precedence" means it will apply only after things on both side are already processed, so what you wrote here is identical to

/^[0-9]{10}|[-]$/

which, however, still won't work for the reasons mentioned in other answers, as is clear from its visualization:

这篇关于仅限数字和连字符的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆