简体   繁体   中英

Regex options in Java String.matches()

I want to add the option 'x' after my regex to ignore white space when using String.matches() in java. However, I see this on http://www.regular-expressions.info/java.html :

The Java String class has several methods that allow you to perform an operation using a regular expression on that string in a minimal amount of code. The downside is that you cannot specify options such as "case insensitive" or "dot matches newline".

Does anyone have an easy way around this using java, so that I don't have to change my regex to allow zero or more white space in every spot there could be white space?

An easy way is to use Pattern class instead of just using the matches() method.

For example:

Pattern ptn = Pattern.compile("[a-z]+", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
Matcher mtcher = ptn.matcher(myStr)
....

I think the website you linked to is inaccurate. Look at the JavaDoc for the multiline flag (m) , the dot-all flag (s) , and the comments flag (x) .

Using the Pattern class, you can specify option flags as a second parameter to the compile method, as pointed out by Alvin:

Pattern.compile("[a-z]+", Pattern.CASE_INSENSITIVE).matcher("Hello").matches() // true

However, this does not help us if the regex must be a string. When it's in a configuration file, for example. Luckily, there is another way

Embedded Flag Expressions

It's also possible to enable various flags using embedded flag expressions. Embedded flag expressions are an alternative to the two-argument version of compile, and are specified in the regular expression itself.

Enter your regex: (?i)foo
Enter input string to search: FOOfooFoOfoO
I found the text "FOO" starting at index 0 and ending at index 3.
I found the text "foo" starting at index 3 and ending at index 6.
I found the text "FoO" starting at index 6 and ending at index 9.
I found the text "foO" starting at index 9 and ending at index 12.

The embedded flag expressions that correspond to Pattern's publicly accessible fields are presented in the following table:

Constant                    Equivalent Embedded Flag Expression
Pattern.CANON_EQ            None
Pattern.CASE_INSENSITIVE    (?i)
Pattern.COMMENTS            (?x)
Pattern.MULTILINE           (?m)
Pattern.DOTALL              (?s)
Pattern.LITERAL             None
Pattern.UNICODE_CASE        (?u)
Pattern.UNIX_LINES          (?d)

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