求正则匹配xml的表达式

onlock 2011-08-12 10:45:12
有这个样一个xml节点如下
<ms id='001' to='test@127.0.0.1;test1@127.0.0.1' from='test2@127.0.0.1'>
<body>你好</body>
<x>xyz</x>
</ms>
现在我需要匹配出to属性的内容,也就是test@127.0.0.1;test1@127.0.0.1这个。
求正则表达式怎么写,首先声明我不用dom4j来解析,就用正则匹配,麻烦各位前辈
指点,本人接触正则不久,不是很熟,另外也不知道能不能这样匹配。
...全文
201 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
soli11722984 2011-08-12
  • 打赏
  • 举报
回复

String str = "<ms id='001' to=\"test@127.0.0.1'test1@127.0.0.1\" from='test2@127.0.0.1'><body>你好</body><x>xyz</x></ms>";
Pattern p = Pattern.compile("to=(((?:')[^']*(?:'))|(?:\")[^\"]*(?:\"))");
Matcher m = p.matcher(str);
while(m.find()){
System.out.println(m.group());
}
soli11722984 2011-08-12
  • 打赏
  • 举报
回复
to=(['|\"])[^('|\")]*\\1")

不过这个是有点问题的

比如说
to='test@127.0.0.1"test1@127.0.0.1'
这就找不出來了,等其他高手解决。。。。。
onlock 2011-08-12
  • 打赏
  • 举报
回复
请问如果to属性是双引号的怎么写?
to=“test@127.0.0.1;test1@127.0.0.1”
这样
onlock 2011-08-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 softroad 的回复:]
引用 1 楼 soli11722984 的回复:

to='([^']*)'
[/Quote]
有何高见,1楼的差不多也可以
softroad 2011-08-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 soli11722984 的回复:]

to='([^']*)'
[/Quote]
soli11722984 2011-08-12
  • 打赏
  • 举报
回复
to='([^']*)'
soli11722984 2011-08-12
  • 打赏
  • 举报
回复

public static void main(String[] args) {
String str = "<ms id='001' to=\"test@127.0.0.1'test1@127.0.0.1\" from='test2@127.0.0.1'><body>你好</body><x>xyz</x></ms>";
Pattern p = Pattern.compile("((?<=to=')([^']*)(?='))|((?<=to=\")([^\"]*)(?=\"))");
Matcher m = p.matcher(str);
while(m.find()){
System.out.println(m.group());
}
}
onlock 2011-08-12
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 soli11722984 的回复:]
Java code


String str = "<ms id='001' to=\"test@127.0.0.1'test1@127.0.0.1\" from='test2@127.0.0.1'><body>你好</body><x>xyz</x></ms>";
Pattern p = Pattern.compile("to=(((?:')([^']*)(……
[/Quote]
首先说声谢谢,已经帮了不少忙了,但我需要的就是test@127.0.0.1;test1@127.0.0.1这个,to不要
soli11722984 2011-08-12
  • 打赏
  • 举报
回复

String str = "<ms id='001' to=\"test@127.0.0.1'test1@127.0.0.1\" from='test2@127.0.0.1'><body>你好</body><x>xyz</x></ms>";
Pattern p = Pattern.compile("to=(((?:')([^']*)(?:'))|((?:\")([^\"]*)(?:\")))");
Matcher m = p.matcher(str);
while(m.find()){
System.out.println(m.group());
}


该了点

51,408

社区成员

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

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