在 Java regex.Pattern 中设置两个标志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18332117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set two flags in Java regex.Pattern
提问by davide
I need a matcher like this:
我需要一个这样的匹配器:
Matcher kuchen = Pattern.compile("gibt es Kuchen in der K\u00FCche",Pattern.CASE_INSENSITIVE).matcher("");
and the problem is that it is not simple ASCII. I know that in this particular case I could use [\u00FC\u00DC] for the ü, but I need to be a bit more general (building the regex from other matcher groups). So according to javadocs:
问题是它不是简单的 ASCII。我知道在这种特殊情况下我可以使用 [\u00FC\u00DC] 作为 ü,但我需要更通用一些(从其他匹配器组构建正则表达式)。所以根据javadocs:
By default, case-insensitive matching assumes that only characters in the US-ASCII charset are being matched. Unicode-aware case-insensitive matching can be enabled by specifying the UNICODE_CASE flag in conjunction with this flag.
默认情况下,不区分大小写的匹配假定只匹配 US-ASCII 字符集中的字符。通过将 UNICODE_CASE 标志与此标志一起指定,可以启用 Unicode 感知的不区分大小写的匹配。
Can anybody tell me how to specify the two flags in conjunction?
谁能告诉我如何同时指定这两个标志?
采纳答案by Roman C
Try
尝试
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE
it should solve the issue. Or-ing the bitmask you will get compound features.
它应该解决这个问题。Or-ing 位掩码,您将获得复合功能。
回答by Sebastian Redl
It's a bitmask, so you use the bitwise OR operator |
.
这是一个位掩码,因此您可以使用按位 OR 运算符|
。
回答by barfuin
Use bitwise OR, like Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE
.
使用按位或,例如Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE
.
回答by Joop Eggen
Though more pure using parameters, same as "(?iu)gibt es ..."
without parameters. i
= case-insensitive, u
= unicode.
虽然使用参数更纯粹,但与"(?iu)gibt es ..."
不使用参数相同。i
= 不区分大小写,u
= unicode。