在Java正则表达式中组合or和否定?
我试图结合使用“not”和“or”生成一组正则表达式匹配,如下所示:在Java正则表达式中组合or和否定?,java,regex,string-matching,regex-negation,Java,Regex,String Matching,Regex Negation,我试图结合使用“not”和“or”生成一组正则表达式匹配,如下所示: "blah" matching "zero or more of" : "not h" or "any in b,l,a" = false "blah" matching "zero or more of" : "any in b,l,a" or "not h" = false "blah" matching "zero or more of" : "not n" or
"blah" matching "zero or more of" : "not h" or "any in b,l,a" = false
"blah" matching "zero or more of" : "any in b,l,a" or "not h" = false
"blah" matching "zero or more of" : "not n" or "any in b,l,a" = true
"blah" matching "zero or more of" : "any in b,l,a" or "not n" = true
我已经尝试了以下正则表达式,但它们似乎没有达到我想要的效果。我还包括了我对正则表达式的解释:
//first set attempt - turns out to be any of the characters within?
System.out.println("blah".matches("[bla|^h]*")); //true
System.out.println("blah".matches("[^h|bla]*")); //false
System.out.println("blah".matches("[bla|^n]*")); //false
System.out.println("blah".matches("[^n|bla]*")); //false
//second set attempt - turns out to be the literal text
System.out.println("blah".matches("(bla|^h)*")); //false
System.out.println("blah".matches("(^h|bla)*")); //false
System.out.println("blah".matches("(bla|^n)*")); //false
System.out.println("blah".matches("(^n|bla)*")); //false
//third set attempt - almost gives the right results, but it's still off somehow
System.out.println("blah".matches("[bla]|[^h]*")); //false
System.out.println("blah".matches("[^h]|[bla]*")); //false
System.out.println("blah".matches("[bla]|[^n]*")); //true
System.out.println("blah".matches("[^n]|[bla]*")); //false
因此,最后,我想知道以下几点:
正则表达式的细分可能类似于(“not[abc]”或“bc”)*,它将匹配任何类似于
bcbc…
或..
的字符串,其中字符不是a
s、b
s或c
s。我只是选择了“blah”作为一般示例,比如“foo”或“bar”。对于前两个条件,您可以使用:
^(?:[bla]|[^h])*$
^(?:[bla]|[^n])*$
下一步2您可以使用:
^(?:[bla]|[^h])*$
^(?:[bla]|[^n])*$
正则表达式详细信息:
:开始^
:启动非捕获组(?:
:匹配[bla]
b或l或a中的一个:
:或|
:匹配任何非[^h]
h
:结束非捕获组,匹配此组中的0个或多个)*
:结束$
请注意,对于
.matches
,锚点是隐式的,因此您可以省略^
和$
要组合您的条件,请在eg非捕获组中使用单独的可选字符集[],以便
“[bla | ^h]*”
将
(?:[bla]*.[^h]*)+
这类似于“至少出现一次(b,l,a或非h)”
请记住,与*
匹配意味着“可能发生”(技术上为零或更多)可以用多种方式编写“非h”:
(?!.*h.*)
[^h]*
“b、l、a中的任何内容”1:
1) 假设你的意思是“b,l,a中只有一个”,否则问题中的所有4个示例都是true
使用或组合将是:
[^h]*|[bla]*
这意味着“必须是不包含h
的字符串,或者必须是仅包含b
、l
和a
字符的字符串
在这种情况下,
的顺序没有区别,因此[^h]*.[bla]*
和[bla]*.[^h]*
的工作原理相同
System.out.println("blah".matches("[bla]*|[^h]*")); //false
System.out.println("blah".matches("[^h]*|[bla]*")); //false
System.out.println("blah".matches("[bla]*|[^n]*")); //true
System.out.println("blah".matches("[^n]*|[bla]*")); //true
这不是消极的前瞻,因为这是在避免将来的元素。我只是想检查当前元素是否匹配。@Turing85它是否定的,但在[]
FYI的字符集上下文中:[^h|bla]
表示“不是h
,
,b
,l
,或a
然而,^
只在第一个位置有特殊的含义,所以[bla | ^h]
的意思是“ab
,l
,a
,
,^
或h
”@Andreas哦,听起来有问题。我以后会记住这一点:)总体评论:您当前对正则表达式的语义是:如果一个字符既不是'b'
,'l'
也不是'a'
,那么它就不能是'h'
。换句话说:字符可以是任何东西,除了h
。这真的是你想要的吗?[^h]
允许b's、l's和a's以及h's以外的任何东西,所以[bla].[^h]
是多余的,当然不是你想要的。你是对的,OP的要求本身是模糊的。@Avi:你需要澄清要求,因为h不满足[bla
因此不需要任何或条件。根据我的要求,这应该是正确的。不捕获组可能不是必需的,但这没关系。很高兴知道,但请记住,^[^h]*$
也适用于您。[^h]
允许b、l和a以及除h以外的任何内容,所以[bla]|[^h]
是非常多余的,当然不是你想要的want@Andreas你想让我纠正OP的要求吗?@Antoniossss你的正则表达式没有编译。一开始单独做什么?它丢失了(?:
@Antoniossss啊,没有捕获?-1已删除