Java RegEx 不区分大小写吗?
在Java中,当执行replaceAll来查找正则表达式模式时,例如:(
replaceAll("\\?i\\b(\\w+)\\b(\\s+\\1)+\\b", "$1");
删除重复的连续的不区分大小写的单词,例如Test测试),我不确定将?i
放在哪里。我读到它应该在开头,但是如果我把它拿出来,那么我会捕获重复的连续单词(例如测试测试),但不会捕获不区分大小写的单词(例如测试测试)。所以我想我可以在开头添加 ?i 但这似乎并不能完成工作。有什么想法吗?谢谢!
In Java, when doing a replaceAll to look for a regex pattern like:
replaceAll("\\?i\\b(\\w+)\\b(\\s+\\1)+\\b", "$1");
(to remove duplicate consecutive case-insensitive words, e.g. Test test), I'm not sure where I put the ?i
. I read that it is supposed to be at the beginning, but if I take it out then i catch duplicate consecutive words (e.g. test test), but not case-insensitive words (e.g. Test test). So I thought I could add the ?i in the beginning but that does not seem to get the job done. Any thoughts? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您还可以匹配不区分大小写的正则表达式,并使用 Pattern.CASE_INSENSITIVE 常量使其更具可读性,例如:
You can also match case insensitive regexs and make it more readable by using the Pattern.CASE_INSENSITIVE constant like:
RegexBuddy 告诉我如果你想在开头包含它,这是正确的语法:
RegexBuddy is telling me if you want to include it at the beginning, this is the correct syntax:
是的,可以在 Java 正则表达式中随意启用和禁用不区分大小写。
看起来您想要这样的东西:
请注意嵌入
Pattern.CASE_INSENSITIVE
标志是(?i)
而不是\?i
。另请注意,一个多余的\b
已从模式中删除。(?i)
放置在模式的开头以启用不区分大小写。在这种特殊情况下,它不会在模式的后面被覆盖,因此实际上整个模式不区分大小写。值得注意的是,事实上您可以将不区分大小写限制为整个模式的一部分。因此,将其放在哪里的问题实际上取决于规范(尽管对于这个特定问题来说并不重要,因为
\w
不区分大小写。为了演示,这里有一个类似的折叠运行示例像
"AaAaaA"
到"A"
现在假设我们指定只有以大写字母开头的行才应该折叠。将
(?i)
放在适当的位置:更一般地,您可以根据需要启用和禁用模式中的任何标志。
另请参阅
java.util.regex.Pattern
/(?i)regex/
来代替/regex/i
(Java 中的Pattern.CASE_INSENSITIVE
)/(?i)regex/李>/first(?i)second(?-i)third/
/first(?i:second)third/
\w
和\s
之间始终有一个\b
)相关问题
Yes, case insensitivity can be enabled and disabled at will in Java regex.
It looks like you want something like this:
Note that the embedded
Pattern.CASE_INSENSITIVE
flag is(?i)
not\?i
. Note also that one superfluous\b
has been removed from the pattern.The
(?i)
is placed at the beginning of the pattern to enable case-insensitivity. In this particular case, it is not overridden later in the pattern, so in effect the whole pattern is case-insensitive.It is worth noting that in fact you can limit case-insensitivity to only parts of the whole pattern. Thus, the question of where to put it really depends on the specification (although for this particular problem it doesn't matter since
\w
is case-insensitive.To demonstrate, here's a similar example of collapsing runs of letters like
"AaAaaA"
to just"A"
.Now suppose that we specify that the run should only be collapsed only if it starts with an uppercase letter. Then we must put the
(?i)
in the appropriate place:More generally, you can enable and disable any flag within the pattern as you wish.
See also
java.util.regex.Pattern
/regex/i
(Pattern.CASE_INSENSITIVE
in Java), you can do/(?i)regex/
/first(?i)second(?-i)third/
/first(?i:second)third/
\b
between a\w
and a\s
)Related questions
如果整个表达式不区分大小写,则只需指定
CASE_INSENSITIVE
标志:If your whole expression is case insensitive, you can just specify the
CASE_INSENSITIVE
flag:您还可以将要检查模式匹配的初始字符串改为小写。并分别在您的模式中使用小写符号。
You also can lead your initial string, which you are going to check for pattern matching, to lower case. And use in your pattern lower case symbols respectively.