Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 548
gsingh2011 Avatar asked Dec 28 '22 07:12

gsingh2011


1 Answers

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)
....
like image 75
Alvin Avatar answered Dec 29 '22 21:12

Alvin