matches() 方法返回 true,因为它需要完整的字符串匹配。您说您在 regex101.com 上测试了正则表达式,但您忘记添加锚点来模拟 matches() 行为。
查看regex proof,您的正则表达式匹配整个字符串。
如果你想停止用这个表达式匹配整个字符串,不要使用.*?,这个模式可以匹配很多。
使用
(?s)(\+?\"[^\"\\]*(?:\\.[^\"\\]*)*\")(\s+[aA][nN][dD]\s+\+?\"[^\"\\]*(?:\\.[^\"\\]*)*\")*
转义版本:
String regex = "(?s)(\\+?\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\")(\\s+[aA][nN][dD]\\s+\\+?\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\")*";
解释
--------------------------------------------------------------------------------
(?s) set flags for this block (with . matching
\n) (case-sensitive) (with ^ and $
matching normally) (matching whitespace
and # normally)
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
\+? '+' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
\" '"'
--------------------------------------------------------------------------------
[^\"\\]* any character except: '\"', '\\' (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
--------------------------------------------------------------------------------
\\ '\'
--------------------------------------------------------------------------------
. any character
--------------------------------------------------------------------------------
[^\"\\]* any character except: '\"', '\\' (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
\" '"'
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
( group and capture to \2 (0 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
[aA] any character of: 'a', 'A'
--------------------------------------------------------------------------------
[nN] any character of: 'n', 'N'
--------------------------------------------------------------------------------
[dD] any character of: 'd', 'D'
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\+? '+' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
\" '"'
--------------------------------------------------------------------------------
[^\"\\]* any character except: '\"', '\\' (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
--------------------------------------------------------------------------------
\\ '\'
--------------------------------------------------------------------------------
. any character
--------------------------------------------------------------------------------
[^\"\\]* any character except: '\"', '\\' (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
\" '"'
--------------------------------------------------------------------------------
)* end of \2 (NOTE: because you are using a
quantifier on this capture, only the LAST
repetition of the captured pattern will be
stored in \2)