Addition of Pattern.compile the Pattern (String REGEX),
the Pattern class compile () There is another version:
the Pattern Pattern.complie (String REGEX, int In Flag) , which accepts a parameter marker flag, to adjust the matching behavior.
Pattern flag from the following class constants:
Compiler flags | effect |
---|---|
Pattern.CANON_EQ | Two characters if and only if they are completely decomposed to match the specification, they are considered a match, for example, if we specify this flag, the expression a \ u030A will match the string? . By default, the standard does not consider matching equivalence |
Pattern.CASE_INSENSITIVE(?i) | By default, case-insensitive matching assumes that only US-ASCII character set can take place. This tag allows pattern matching without considering the case (upper or lower case). By specifying UNICODE_CASE marks and combine this tag, case-insensitive Unicode-based matching can open, you can also use the embedded tag expression? I open, the same below |
Pattern.COMMENTS(?x) | In this mode, the expression of space ( not referring to \ S , merely referring to spaces) will be ignored, and beginning with # comment until the end of the line will be ignored. Expressions by embedding tag line mode can also be turned in Unix |
Pattern.DOTALL(?s) | In dotall mode, the expression. "" Matches all characters, including the line terminator. By default, "." Expression does not match line terminators |
Pattern.MULTLINE(?m) | In the multi-line mode, and expression ^ $ matching row or respectively the beginning and ending of the string. By default, these expressions to match only the beginning of a string of input and complete end |
Pattern.UNICODE_CASE(?u) | When specifying the tag, and opens CASE_INSENSITIVE , the case-insensitive match will manner consistent with the Unicode standard. By default, case-insensitive matching assumes that only the US-ASCII character set to |
Pattern.UNIX_LINES(?d) | In this mode,., ^ $ Behavior and recognizes only the line terminator \ n |
In these tags Pattern.CASE_INSENSITIVE(?i) ,Pattern.MULTLINE(?m), == Pattern.COMMENTS (? X) == particularly useful.
Use example:
we can "or" | functional operation of combining a plurality of markers ()
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ReFlags {
public static void main(String[] args) {
Pattern p=Pattern.compile("^java",Pattern.CASE_INSENSITIVE|Pattern.MULTILINE);
/*
* 使用Pattern.CASE_INSENSITIVE(大小写不敏感的匹配)和Pattern.MULTILINE(多行模式)标记,忽略大小写地匹配所有以java开头的行
*/
Matcher m=p.matcher("java has regex\nJava has regex\n"
+ "JAVA has pretty good regular expression\n"
+ "Regular expressions are in JavA");
while (m.find()) {
System.out.println(m.group());//输出已匹配的部分
}
}
}
Output:
java
Java
JAVA
Use Pattern.COMMENTS (? X) examples:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ReFlags_Comments {
public static void main(String[] args) {
/*
* 不使用Pattern.COMMENTS(不启动注释)
*/
String s="123";
Pattern p1=Pattern.compile(" (\\d+)+#test comments");
Matcher m1=p1.matcher(s);
System.out.println(m1.matches());//false
/*
* 正则表达式中使用启动注释的标记
*/
Pattern p2=Pattern.compile("(?x) (\\d+)+#test comments");
Matcher m2=p2.matcher(s);
System.out.println(m2.matches());//true
/*
* 参数中使用Pattern.COMMENTS以启动注释
*/
Pattern p3=Pattern.compile(" (\\d+)+#test comments",Pattern.COMMENTS);
Matcher m3=p3.matcher(s);
System.out.println(m3.matches());//true
}
}
operation result:
false
true
true
Reference books: "Thinking in Java"