Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pattern.compile with no flags?

Tags:

java

regex

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)?

like image 802
elite5472 Avatar asked Dec 10 '22 00:12

elite5472


2 Answers

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...

like image 57
Jon Skeet Avatar answered Jan 02 '23 10:01

Jon Skeet


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.

like image 38
Mike Samuel Avatar answered Jan 02 '23 10:01

Mike Samuel