繁体   English   中英

正则表达式大于或小于3位数和字符

[英]Regex for greater than or less than 3 digits and characters

这可能只是通常的“哦,不要看另一个人要求正则表达式”,但我真的无法想象这一个。 我需要做的是找到一个简单的正则表达式,它将匹配任何大于或小于3位的数据。 它也必须匹配字符。

只是一点解释。 我试图匹配任何不是电话号码的标准区号的东西。 所以> 3 <包括字符。 我将其用于业务规则,并且已经与区号的正面版本匹配。

一次只能通过正则表达式传递一条记录,因此不需要分隔符。

好的抱歉,这里有一些例子:

337   : does not match
123   : does not match
12    : does match
1     : does match
asd   : does match
as2   : does match
12as45: does match
1234  : does match

相反的情况非常简单,只能是[0-9] {3}或[\\ d] {3}。

PS它在java中

现在这是“az”的解决方案(因为它似乎很常见):

^(?!\d{3})[a-z0-9]{3}$|^[a-z0-9]{1,2}$|^[a-z0-9]{4,}$

...这是真正的解决方案,它匹配除了三个全部数字的字符之外的所有内容:

^(?!\d{3}).{3}$|^.{1,2}$|^.{4,}$

http://regexr.com?358u9

因为我们只是检查三种选择,所以它非常自我解释。

你可以这样做

^(?:(?!(?<!\d)\d{3}(?!\d))[a-zA-Z\d])+$

在Regexr上看到它。

解释

^                                # match the start of the string (not needed with the matches() method)
    (?:                          # start of a non capturing group
        (?!(?<!\d)\d{3}(?!\d))   # combined lookarounds, fails if there are 3 digits following with not a digit before and ahead of those 3 digits
        [a-zA-Z\d]               # match one ASCII letter or digit
    )+                           # repeat this at least once
$                                # match the end of the string (not needed with the matches() method)

使用\\ d {3}使用负面看法: http//www.regular-expressions.info/lookaround.html

暂无
暂无

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

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