我想在我的正则表达式后添加选项“x”,以便在 java 中使用 String.matches() 时忽略空格。但是,我在 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".
有没有人有使用 java 解决此问题的简单方法,这样我就不必更改我的正则表达式以在每个可能有空白的地方允许零个或多个空白?
最佳答案
一个简单的方法是使用 Pattern类而不是仅仅使用 matches() 方法。
例如:
Pattern ptn = Pattern.compile("[a-z]+", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
Matcher mtcher = ptn.matcher(myStr)
....
关于java - Java String.matches() 中的正则表达式选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8950019/