Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 大于或小于3个数字和字符的正则表达式_Java_Regex_Regex Negation - Fatal编程技术网

Java 大于或小于3个数字和字符的正则表达式

Java 大于或小于3个数字和字符的正则表达式,java,regex,regex-negation,Java,Regex,Regex Negation,这可能只是通常的“哦,不,看另一个人要正则表达式”,但我真的搞不懂这个。我需要做的是找到一个简单的正则表达式,它将匹配任何大于或小于3位的数字。但它也必须匹配字符 只是一个小小的解释。我正在尝试匹配任何不是电话号码标准区号的内容。so>3

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

只是一个小小的解释。我正在尝试匹配任何不是电话号码标准区号的内容。so>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}


注意:在java中,Its使用带\d{3}的反向查找:

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

。。。这是真正的解决方案,除了三个全是数字的字符外,其他字符都匹配:

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

因为我们只检查了三个备选方案,这是不言自明的。

您可以这样做

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

解释

^                                # 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)
^#匹配字符串的开头(matches()方法不需要)
(?:#非捕获组的开始

(?!(?向我们展示您想要匹配的内容的“积极”版本。还有一些好字符串和坏字符串的示例。这是什么语言?难道不可能只使用“积极”吗正则表达式并使用语言功能否定它?你的小小解释不足以回答这个问题。原则上,没有理由解释常规语法不能支持集合差异(任何语法或正则表达式都代表一组字符串,因此你可以从任意长度的数字字符串集合中减去3位字符串的集合)但我不知道这是否是正则表达式的常见运算符——尽管这可能是Nitzan的建议?@stema很抱歉,答案已被编辑。嗨,我需要的是“一到两个或四个以上的数字或字符”因此,它将匹配:a、as1、121vc、12、1234等等..好的,但是这些数字和字符序列是用什么字符分隔的?我假设是用空格分隔的?看看我编辑过的问题。很抱歉没有进一步解释。没有分隔符。一次只传递一条记录,所以不必担心被编辑。B但请注意,这与标题相矛盾,标题上写着“在字符串中”。字符串中必须有分隔符。现在它正在从头到尾检查整个字符串。哦,现在示例显示了其他内容。我稍后将对此进行研究。谢谢:)真的很有帮助。终于让我的规则起作用了。要不要解释一下否决票?这几乎是stema的答案,被接受了。
^                                # 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)