求助正则表达式。

bijiDango 2013-09-17 12:12:35
可以在字符串中设置正则表达式的参数吗

比如"a.*b"这个表达式,我想设置为MULTILINE,那么字符串要怎么写

这个功能在js里面有,java应该也可以吧!
...全文
113 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
bijiDango 2013-09-18
  • 打赏
  • 举报
回复
引用 4 楼 bayougeng 的回复:
你要用的就是 (?idmsux-idmsux) Nothing, but turns match flags i d m s u x on - off 第一个是CASE_INSENSITIVE,第二个是comments,第三个是.号匹配multiline……
求举例 "a.*b"加上CASE_INSENSITIVE,MULTILINE,LITERAL组合出来长什么样?
suciver 2013-09-18
  • 打赏
  • 举报
回复
引用 7 楼 zhengdk87883556 的回复:
[quote=引用 6 楼 suciver 的回复:] [quote=引用 5 楼 zhengdk87883556 的回复:] [quote=引用 4 楼 bayougeng 的回复:] 你要用的就是 (?idmsux-idmsux) Nothing, but turns match flags i d m s u x on - off 第一个是CASE_INSENSITIVE,第二个是comments,第三个是.号匹配multiline……
求举例 "a.*b"加上CASE_INSENSITIVE,MULTILINE,LITERAL组合出来长什么样?[/quote] java中CASE_INSENSITIVE对应的是(?i),MULTILINE对应的是(?m),LITERAL不存在可以启用字面值解析的嵌入式标志字符。 [/quote] "a.*b(?i)(?m)"这样吗? 顺便为什么不能literal,比如strA = "1+2+3" strB = "2+3"而你想strA.replaceAll(strB, "5")怎么办 [/quote] 同时启用CASE_INSENSITIVE,MULTILINE是(?im)a.*b java中不是不能literal是正则中没有对应的?标识,你要使用literal就要这么用

String a="1+2+3";
String b="2+3";
//使用java.util.Pattern可以支持标识
Matcher m=Pattern.compile(b, Pattern.LITERAL).matcher(a);
StringBuffer sb=new StringBuffer();
while(m.find()){
  m.appendReplacement(sb, "5");
}
m.appendTail(sb);
System.out.println(sb.toString());
bijiDango 2013-09-18
  • 打赏
  • 举报
回复
引用 6 楼 suciver 的回复:
[quote=引用 5 楼 zhengdk87883556 的回复:] [quote=引用 4 楼 bayougeng 的回复:] 你要用的就是 (?idmsux-idmsux) Nothing, but turns match flags i d m s u x on - off 第一个是CASE_INSENSITIVE,第二个是comments,第三个是.号匹配multiline……
求举例 "a.*b"加上CASE_INSENSITIVE,MULTILINE,LITERAL组合出来长什么样?[/quote] java中CASE_INSENSITIVE对应的是(?i),MULTILINE对应的是(?m),LITERAL不存在可以启用字面值解析的嵌入式标志字符。 [/quote] "a.*b(?i)(?m)"这样吗? 顺便为什么不能literal,比如strA = "1+2+3" strB = "2+3"而你想strA.replaceAll(strB, "5")怎么办
suciver 2013-09-18
  • 打赏
  • 举报
回复
引用 5 楼 zhengdk87883556 的回复:
[quote=引用 4 楼 bayougeng 的回复:] 你要用的就是 (?idmsux-idmsux) Nothing, but turns match flags i d m s u x on - off 第一个是CASE_INSENSITIVE,第二个是comments,第三个是.号匹配multiline……
求举例 "a.*b"加上CASE_INSENSITIVE,MULTILINE,LITERAL组合出来长什么样?[/quote] java中CASE_INSENSITIVE对应的是(?i),MULTILINE对应的是(?m),LITERAL不存在可以启用字面值解析的嵌入式标志字符。
bayougeng 2013-09-17
  • 打赏
  • 举报
回复
你要用的就是 (?idmsux-idmsux) Nothing, but turns match flags i d m s u x on - off 第一个是CASE_INSENSITIVE,第二个是comments,第三个是.号匹配multiline……
bayougeng 2013-09-17
  • 打赏
  • 举报
回复
MULTILINE public static final int MULTILINEEnables multiline mode. In multiline mode the expressions ^ and $ match just after or just before, respectively, a line terminator or the end of the input sequence. By default these expressions only match at the beginning and the end of the entire input sequence. Multiline mode can also be enabled via the embedded flag expression (?m). See Also: Constant Field Values -------------------------------------------------------------------------------- 这个就是java doc里的multiline Special constructs (non-capturing) (?:X) X, as a non-capturing group (?idmsux-idmsux) Nothing, but turns match flags i d m s u x on - off
bijiDango 2013-09-17
  • 打赏
  • 举报
回复
引用 1 楼 ghostkngiht 的回复:
(?a.*b)
这不能解决所有问题啊,比如还还想设置LITERAL呢
ghostkngiht 2013-09-17
  • 打赏
  • 举报
回复
(?a.*b)

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧