我正在尝试结合使用“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 "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
所以,最后,我想知道以下几点:
我对上述正则表达式的解释是否正确?
符合我的规范的一组四个 Java 正则表达式是什么?
(可选)我是否在我的正则表达式中犯了其他错误?
关于模糊要求,我只想提出以下几点:正则
表达式细分可能类似于 ("not [abc]" or "bc")* ,它会匹配任何类似的字符串bcbc...
或...
字符所在的位置不是a
s、b
s 或c
s。我只是选择“blah”作为一般示例,例如“foo”或“bar”。
侃侃无极
POPMUISE
慕工程0101907
相关分类