Mega Code Archive

 
Categories / Java / Internationalization
 

Matching Line Boundaries in a Regular Expression

import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main {   public static void main(String[] argv) throws Exception {     CharSequence inputStr = "abc\ndef";     String patternStr = "abc$";     // Compile with multiline enabled     Pattern pattern = Pattern.compile(patternStr, Pattern.MULTILINE);     Matcher matcher = pattern.matcher(inputStr);     boolean matchFound = matcher.find(); // true     // Use an inline modifier to enable multiline mode     matchFound = pattern.matches(".*abc$.*", "abc\r\ndef"); // false     matchFound = pattern.matches("(?m).*abc$.*", "abc\r\ndef"); // true   } }