matches()
method returns true because it needs a full string match. You say you tested the regular expression on regex101.com, but you forgot to add anchors to simulate matches()
behavior.
See regex proof that your regex matches the whole string.
If you want to stop matching the entire string with this expression, do not use .*?
, this pattern can match really a lot.
Use
(?s)(\+?\"[^\"\\]*(?:\\.[^\"\\]*)*\")(\s+[aA][nN][dD]\s+\+?\"[^\"\\]*(?:\\.[^\"\\]*)*\")*
Escaped version:
String regex = "(?s)(\\+?\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\")(\\s+[aA][nN][dD]\\s+\\+?\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\")*";
EXPLANATION
--------------------------------------------------------------------------------
(?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)