简体   繁体   English

正则表达式缺失模式

[英]regular expression missing pattern

I try to create a regex (PHP) that allow all digits,and allow only closed brackets with [digit-digit] format for exp. 我尝试创建一个允许所有数字的正则表达式(PHP),并且仅允许exp具有[digit-digit]格式的方括号。

0[1-3]xxx xx //valid
[1-3]xxx xx //valid
05[1-3]xxx xx //valid
0[1-3]555 6789 //valid
0332 555 6789 //valid

0[1-11]xxx xx //not valid
[1-3]xxxa xx //not valid

My regex pattern, [0-9]?\\[[0-9]-[0-9]\\][0-9 x]+ , validates the following: 我的正则表达式模式[0-9]?\\[[0-9]-[0-9]\\][0-9 x]+验证以下内容:

[0-5]212xxx xx xx
[0-5]212xx xx xx
[0-5]212xx xx xx

...but it does not validate this string: ...但是它不会验证此字符串:

1231232131 

Change regex completely or edit the regex already but it seems getting complicated. 完全更改正则表达式或已经编辑正则表达式,但是看起来变得很复杂。 How can I achieve my goal with most efficient way? 如何以最有效的方式实现我的目标?

This pattern tests that string contains digit, construction [digit-digit] , space and character x 此模式测试字符串包含数字,结构[digit-digit] ,空格和字符x

 ^(\d|\[\d-\d\]|x| )+$

or you can write it so: 或者您可以这样写:

^([\dx ]|\[\d-\d\])+$

demo and explanation 演示和说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM