I have the following code:
String example = "<!--§FILES_SECTION§\n" +
"Example line one\n" +
"Example line two\n" +
"§FILES_SECTION§-->";
String myPattern = ".*?FILES_SECTION.*?\n(.*?)\n.*?FILES_SECTION.*?";
Pattern p = Pattern.compile(myPattern);
Matcher m = p.matcher(example);
if ( m.matches() )
Log.d("Matcher", "PATTERN MATCHES!");
else
Log.d("MATCHER", "PATTERN DOES NOT MATCH!");
Why does it always return "PATTERN DOES NOT MATCH?"
By default, the . does not match line breaks. You would need to add a regex option so that it does:
Pattern p = Pattern.compile(myPattern,Pattern.DOTALL);
m.matches() will only return true if the entire string matches. Use m.find() instead, and it should work better!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With