You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Pattern.compile()中同时使用CASE_INSENSITIVE与MULTILINE模式

如何在Pattern.compile()中同时指定CASE_INSENSITIVE和MULTILINE模式

嘿,这事儿其实挺简单的!Java的Pattern类里的模式常量都是位掩码类型的整数,所以你只需要用**位或运算符(|)**把多个模式常量组合起来传给compile()方法就行。

你原来的代码是这样的:

Pattern tokenPattern = Pattern.compile("Hi", Pattern.CASE_INSENSITIVE);

要同时加上MULTILINE模式,只需要把两个常量用|连接:

Pattern tokenPattern = Pattern.compile("Hi", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);

顺便提一嘴,MULTILINE模式的作用是让正则表达式里的^和$匹配输入字符串中每一行的开头和结尾,而不是默认的整个字符串的首尾。如果之后你还需要添加其他模式(比如UNICODE_CASE来支持Unicode字符的大小写不敏感匹配),同样可以继续用|叠加:

Pattern tokenPattern = Pattern.compile("Hi", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.UNICODE_CASE);

内容的提问来源于stack exchange,提问作者saner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:54:32