4

我在文档中找不到这个。我需要启用不区分大小写,但仅在特殊情况下。

如何Pattern.compile(String regex, int flags)以等效于的方式调用该方法Pattern.compile(String regex)?我可以用Pattern.compile("my regex", 0)吗?

4

3 回答 3

7

是的 -Pattern.compile(foo)最终只是返回Pattern.compile(foo, 0)

如果文档真的这么说会很好,但这就是我刚刚看到的实现......

于 2012-04-16T17:39:44.747 回答
5

我可以用Pattern.compile("my regex", 0)吗?

是的。javadoc说_

flags - 匹配标志,可能包括 CASE_INSENSITIVE、MULTILINE、DOTALL、UNICODE_CASE、CANO​​N_EQ、UNIX_LINES、LITERAL、UNICODE_CHARACTER_CLASS 和 COMMENTS 的位掩码

0 is the bitmask containing no bits.


I need to enable case insensitivity, but only in special cases.

There are a few different kinds of case-sensitivity available with Pattern.

For more fine-grained control over case-sensitivity, you might need to do your own case folding or collation.

于 2012-04-16T17:42:02.533 回答
2

代码为Pattern.compile(String regex)(第1021 行及以下):

public static Pattern compile(String regex) {
    return new Pattern(regex, 0);
}

代码Pattern.compile(String regex, int flags)是:

public static Pattern compile(String regex, int flags) {
    return new Pattern(regex, flags);
}

所以是的。

于 2012-04-16T17:40:07.957 回答