本文实例讲述了Java使用正则表达式截取重复出现的XML字符串功能。分享给大家供大家参考,具体如下:
1
2
3
4
5
6
7
8
9
|
public static void main(String[] args) throws DocumentException { String s = "<MTCCommand Time=\"2014-03-22\"><Login UserId=\"admin\" UserPasswd=\"123\" /></MTCCommand><MTCCommand><Login/></MTCCommand>" ; String regEx = "<MTCCommand((?!<MTCCommand).)*</MTCCommand>" ; Pattern pat = Pattern.compile(regEx); Matcher mat = pat.matcher(s); while (mat.find()){ System.out.println(mat.group()); } } |
输出结果:
1
2
|
< MTCCommand Time = "2014-03-22" >< Login UserId = "admin" UserPasswd = "123" /></ MTCCommand > < MTCCommand >< Login /></ MTCCommand > |
希望本文所述对大家java程序设计有所帮助。
原文链接:http://blog.csdn.net/lotusyangjun/article/details/22187975