简体   繁体   English

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

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

This is probably just the usual "Oh no look another person asking for regex" but I really cant figure this one out. 这可能只是通常的“哦,不要看另一个人要求正则表达式”,但我真的无法想象这一个。 What I need to do is find a simple regex that will match anything that's greater than or less than 3 digits. 我需要做的是找到一个简单的正则表达式,它将匹配任何大于或小于3位的数据。 It must also match characters though. 它也必须匹配字符。

Just a little explanation. 只是一点解释。 I am trying to match anything that isn't the standard area code for a telephone number. 我试图匹配任何不是电话号码的标准区号的东西。 so > 3 < including characters. 所以> 3 <包括字符。 I am using this for business rules and have already matched the positive version of the area code. 我将其用于业务规则,并且已经与区号的正面版本匹配。

only one record is passed through the regex at one time so there are no need for delimiters. 一次只能通过正则表达式传递一条记录,因此不需要分隔符。

Okay sorry about that here are some examples: 好的抱歉,这里有一些例子:

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

the opposite is really easy and can just be [0-9]{3} or [\\d]{3}. 相反的情况非常简单,只能是[0-9] {3}或[\\ d] {3}。

PS Its in java PS它在java中

Now this is the solution with "az" (because it seems to be so common): 现在这是“az”的解决方案(因为它似乎很常见):

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

... and this is the true solution, which matches everything except three characters that are all digits: ...这是真正的解决方案,它匹配除了三个全部数字的字符之外的所有内容:

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

http://regexr.com?358u9 http://regexr.com?358u9

Because we are just checking three alternatives, it's pretty self explanatory. 因为我们只是检查三种选择,所以它非常自我解释。

You can do it this way 你可以这样做

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

See it here on Regexr . 在Regexr上看到它。

Explained 解释

^                                # 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