正则表达式截取指定开头和结尾的字符串

wc0077 2012-07-25 10:56:51
String testStr="<SOAP-ENV:Envelope xmlns:SOAP-ENV=http://schemas.xmlsoap.org/soap/envelope/>"
+"<SOAP-ENV:Body>"
+"<samlp:ArtifactResolve xmlns:samlp=urn:oasis:names:tc:SAML:2.0:protocol xmlns:saml=urn:oasis:names:tc:SAML:2.0:assertion ID=id_2 Version=2.0 IssueInstant=2007-12-05T09:22:04Z>"
+"<saml:Issuer>https://idm.10086.cn/SSO</saml:Issuer>"
+"<samlp:Artifact>e4fd0055e1e64077aadf39c98a2814a2</samlp:Artifact>"
+"</samlp:ArtifactResolve>"
+"</SOAP-ENV:Body>"
+"</SOAP-ENV:Envelope>";
截取<SOAP-ENV:Body>和</SOAP-ENV:Body>标签之间的内容,用java的正则表达式实现

在线等求解,急急急!
...全文
1842 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzilihuangtian 2012-12-14
  • 打赏
  • 举报
回复
非常感谢,有用,带走。
wc0077 2012-07-26
  • 打赏
  • 举报
回复
上面这个回复是对brightyq说的,对此深表歉意
wc0077 2012-07-26
  • 打赏
  • 举报
回复
写代码写到半夜,头有点晕,结贴的时候没注意给你分数,真对不起,谢谢你的回复
wc0077 2012-07-26
  • 打赏
  • 举报
回复
非常感谢,很精炼的代码,受教了
qybao 2012-07-25
  • 打赏
  • 举报
回复
for example
testStr = testStr.replaceAll("(?is).*?<SOAP-ENV:Body>(.*?)</SOAP-ENV:Body>.*", "$1");
System.out.println(testStr);
brightyq 2012-07-25
  • 打赏
  • 举报
回复
String testStr="<SOAP-ENV:Envelope xmlns:SOAP-ENV=http://schemas.xmlsoap.org/soap/envelope/>"
+"<SOAP-ENV:Body>"
+"<samlp:ArtifactResolve xmlns:samlp=urn:oasis:names:tc:SAML:2.0:protocol xmlns:saml=urn:oasis:names:tc:SAML:2.0:assertion ID=id_2 Version=2.0 IssueInstant=2007-12-05T09:22:04Z>"
+"<saml:Issuer>https://idm.10086.cn/SSO</saml:Issuer>"
+"<samlp:Artifact>e4fd0055e1e64077aadf39c98a2814a2</samlp:Artifact>"
+"</samlp:ArtifactResolve>"
+"</SOAP-ENV:Body>"
+"</SOAP-ENV:Envelope>";

Pattern p = Pattern.compile("<SOAP-ENV:Body>(.*)</SOAP-ENV:Body>");
Matcher m = p.matcher(testStr);
while(m.find()){
System.out.println(m.group(1));
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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