我是Java正则表达式的新手。如果您发现这个问题真的很简单,我深表歉意。但是我看到了一些类似的问题,并尝试了这些答案,但都没有用。
我正在尝试匹配具有模式“括号外的单词(单词)”的字符串,括号内的内容可能有多种选择,但我想排除其中的一些。换句话说,括号内不包含某些单词的字符串应匹配。
例如,如果我不希望A或B在括号内,
words outside parenthesis ( C ) // should match
words outside parenthesis ( A ) // should not match
words outside parenthesis ( B ) // should not match
其中A和B可以是单词或短语。但是它们不会一起出现(模式“ AB”不可能出现在括号内)。
我试过了:
.+\(\s*(?!A|B)\s*\)
和
.+\(\s*^/(?!A|B)\s*\)
和
.+\(\s*(?!\bA\b)(?!\bB\b)\s*\)
但是,他们都不起作用。请帮忙!谢谢!
编辑:
也许我没有澄清自己。A或B可以是单词或短语,这意味着A可以是单词“ hello”,B可以是“ hello world”,而我不想匹配“ hello”或“ hello world”。所以[^ AB]不适用于我的情况,因为那样会变成[^ hellohello world]。
正则表达式的问题是,在进行否定的前瞻后,(?![^AB])
您不会在)
用a\w+
或[^)]+
like结束之前对单词进行匹配
.+\(\s(?!(nomatch|false)\b)\w+\s\)
匹配样本:
words outside parenthesis ( match ) // matches
words outside parenthesis ( true ) // matches
words outside parenthesis ( nomatch ) // doesn't match
words outside parenthesis ( false ) // doesn't match
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句