I am a little bit confused with this code below:
public class RegularEx {
public static void main(String... arg) {
Pattern pattern = Pattern.compile("M ", 5);
Matcher matcher = pattern.matcher("M Merit Match MM m mM");
while (matcher.find())
System.out.print(matcher.group() " ");
}
}
in this part Pattern.compile("M ", 5);
According to https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#compile-java.lang.String-int- the number should represent flags that may include CASE_INSENSITIVE, MULTILINE, DOTALL, UNICODE_CASE, CANON_EQ, UNIX_LINES, LITERAL, UNICODE_CHARACTER_CLASS and COMMENTS
amount them , 5 should represent what?
Source: https://java.meritcampus.com/core-java-questions/Regular-Expression?t=195
CodePudding user response:
5 is a combination of 1 (UNIX_LINES) and 4 (COMMENTS), the full list of the constants can be found here.
java.util.regex.Pattern
Modifier and Type Constant Field Value
public static final int CANON_EQ 128
public static final int CASE_INSENSITIVE 2
public static final int COMMENTS 4
public static final int DOTALL 32
public static final int LITERAL 16
public static final int MULTILINE 8
public static final int UNICODE_CASE 64
public static final int UNICODE_CHARACTER_CLASS 256
public static final int UNIX_LINES 1
Never specify flags like that, instead do
Pattern pattern = Pattern.compile("M ", UNIX_LINES | COMMENTS);