java - Java String.matches() 中的正则表达式选项

标签 java regex whitespace

我想在我的正则表达式后添加选项“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/

相关文章:

regex - 查看正则表达式断言的背后以匹配嵌套的 HTML 表

c - scanf() 格式字符串中的空白

javascript - 删除 Javascript 中的空格

java - JTable 仅以编程方式排序

c# - CSV 正则表达式拆分缺失的列

java - 如何将泰勒级数写为函数

java - 正则表达式检查数字和字母

Java正则表达式line.split ("\\s*//")

java - Swing:如何创建事件并将它们分派(dispatch)给组件?

java - Android程序设计错误