简体   繁体   中英

Pattern.compile with no flags?

I couldn't find this in the documentation. I need to enable case insensitivity, but only in special cases.

How do I call the method Pattern.compile(String regex, int flags) in such a way that it is equivalent to Pattern.compile(String regex) ? Can I just use Pattern.compile("my regex", 0) ?

Yes - Pattern.compile(foo) ends up just returning Pattern.compile(foo, 0) .

It would be nice if the documentation actually said that, but that's what the implementation I just looked at does...

Can I just use Pattern.compile("my regex", 0) ?

Yes. The javadoc says

flags - Match flags, a bit mask that may include CASE_INSENSITIVE, MULTILINE, DOTALL, UNICODE_CASE, CANON_EQ, UNIX_LINES, LITERAL, UNICODE_CHARACTER_CLASS and COMMENTS

0 is the bitmask containing no bits.


I need to enable case insensitivity, but only in special cases.

There are a few different kinds of case-sensitivity available with Pattern .

For more fine-grained control over case-sensitivity, you might need to do your own case folding or collation.

The code for Pattern.compile(String regex) is (line 1021 and following):

public static Pattern compile(String regex) {
    return new Pattern(regex, 0);
}

and the code for Pattern.compile(String regex, int flags) is:

public static Pattern compile(String regex, int flags) {
    return new Pattern(regex, flags);
}

so yes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM