Java regex Matcher.matches-funksjonen samsvarer ikke med hele strengen
Jeg prøver å matche en hel streng mot et regulært uttrykk, men Matcher.match
funksjonen returnerer sann selv når hele strengen ikke samsvarer.
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main(String[] args) { final String string = ""query1" "query2" "query3""; // Unescaped Pattern: (+?".*?[^\]")(s+[aA][nN][dD]s++?".*?[^\]")* final Pattern QPATTERN = Pattern.compile("(\+?".*?[^\\]")(\s+[aA][nN][dD]\s+\+?".*?[^\\]")*", Pattern.MULTILINE); Matcher matcher = QPATTERN.matcher(string); System.out.println(matcher.matches()); matcher = QPATTERN.matcher(string); while (matcher.find()) { System.out.println("Full match: " + matcher.group(0)); for (int i = 1; i <= matcher.groupCount(); i++) { System.out.println("Group " + i + ": " + matcher.group(i)); } } } }
Du kan se fra while-løkken at regex bare samsvarer med deler av strengen "query1", "query2" og "query3", men ikke hele strengen. Likevel returnerer matcher.matches() true.
Hvor tar jeg feil?
Jeg sjekket mønsteret på https://regex101.com/ også, og hele strengen samsvarer ikke.
Svar
matches()
metoden returnerer true fordi den trenger en fullstendig strengmatch. Du sier at du testet det regulære uttrykket på regex101.com, men du glemte å legge til ankere for å simulere matches()
oppførsel.
Se regex bevis på at regex matcher hele strengen.
Hvis du vil slutte å matche hele strengen med dette uttrykket, ikke bruk .*?
, dette mønsteret kan matche veldig mye.
Bruk
(?s)(+?"[^"\]*(?:\.[^"\]*)*")(s+[aA][nN][dD]s++?"[^"\]*(?:\.[^"\]*)*")*
Escaped versjon:
String regex = "(?s)(\+?"[^"\\]*(?:\\.[^"\\]*)*")(\s+[aA][nN][dD]\s+\+?"[^"\\]*(?:\\.[^"\\]*)*")*";
FORKLARING
-------------------------------------------------------------------------------- (?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)