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

发布于 2024-12-27 22:38:00 字数 401 浏览 2 评论 0原文

我想在正则表达式之后添加选项“x”,以在 java 中使用 String.matches() 时忽略空格。但是,我在 http://www.regular-expressions.info/java.html:

Java String 类有几个方法可以让你执行 在该字符串上使用正则表达式进行最少的操作 代码量。缺点是您无法指定诸如 作为“不区分大小写”或“点匹配换行符”。

有没有人有一个使用java的简单方法来解决这个问题,这样我就不必更改我的正则表达式来允许每个可能有空格的地方有零个或多个空格?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初见终念 2025-01-03 22:38:00

一种简单的方法是使用 模式类而不仅仅是使用 matches() 方法。

例如:

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

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)
....
寄离 2025-01-03 22:38:00

使用 Pattern 类,您可以将选项标志指定为compile 方法的第二个参数,正如 Alvin 所指出的:

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

但是,如果正则表达式必须是字符串,这对我们没有帮助。例如,当它在配置文件中时。幸运的是,还有另一种方法

嵌入式标志表达式

还可以使用嵌入式标志表达式启用各种标志。嵌入式标志表达式是编译的双参数版本的替代方案,并在正则表达式本身中指定。

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.

下表列出了与 Pattern 的可公开访问字段相对应的嵌入式标志表达式:

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)

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)
厌味 2025-01-03 22:38:00

我认为您链接到的网站不准确。查看 JavaDoc 中的 多行标志 ( m)点-所有标志(s)评论标志(x)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文