[英]Regular Expression :match string containing only non repeating words
我有这种情况(Java代码):1)字符串如:“狂野冒险”应匹配。 2)带有相邻重复单词的字符串:“狂野野外冒险”不应该匹配。
使用此正则表达式:。* \\ b(\\ w +)\\ b \\ s * \\ 1 \\ b。*我可以匹配包含相邻重复单词的字符串。
如何扭转这种情况,即如何匹配不包含相邻重复单词的字符串
使用负前瞻断言, (?!pattern)
?! (?!pattern)
。
String[] tests = {
"A wild adventure", // true
"A wild wild adventure" // false
};
for (String test : tests) {
System.out.println(test.matches("(?!.*\\b(\\w+)\\s\\1\\b).*"));
}
REGEX: (?!.*\b(\w+)\s\1\b).*
NODE EXPLANATION
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
\b the boundary between a word char (\w)
and something that is not a word char
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1
or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
\1 what was matched by capture \1
--------------------------------------------------------------------------------
\b the boundary between a word char (\w)
and something that is not a word char
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
只有当你想要积极匹配的其他模式时,否定断言才有意义(参见上面的例子)。 否则,你可以使用布尔补码运算符!
用你之前使用的任何模式否定matches
。
String[] tests = {
"A wild adventure", // true
"A wild wild adventure" // false
};
for (String test : tests) {
System.out.println(!test.matches(".*\\b(\\w+)\\s\\1\\b.*"));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.