用正则表达式取字符串的问题

秋风ss落叶 2003-01-02 01:44:34
如何用正则表达式取出<p>和</p>之间的内容?
例如:

<p>
test1
test2
</p>

我想取出
test1
test2
...全文
28 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
希偌 2003-01-02
  • 打赏
  • 举报
回复
改成下面这个更好些!
public class test {
public static void main(String args[]) {
String str="<p>test1test2</p>";
String str1="(<p>)(.*)(<\\/p>)";
java.util.regex.Pattern p=Pattern.compile(str1);
Matcher m=p.matcher(str);
boolean f=m.find();
StringBuffer sb=new StringBuffer();
while(f) {
m.appendReplacement(sb,"$2");
f=m.find();
}
m.appendTail(sb);
System.out.print(sb.toString());
}
}
希偌 2003-01-02
  • 打赏
  • 举报
回复
jdk1.4以上:
import java.util.regex.*;

public class test {
public static void main(String args[]) {
String str="<p>test1test2</p>";
String str1="(<p>)(.+)(<\\/p>)";
java.util.regex.Pattern p=Pattern.compile(str1);
Matcher m=p.matcher(str);
boolean f=m.find();
StringBuffer sb=new StringBuffer();
while(f) {
m.appendReplacement(sb,"$2");
f=m.find();
}
m.appendTail(sb);
System.out.print(sb.toString());
}
}
newman0708 2003-01-02
  • 打赏
  • 举报
回复
是不是要用到XML解析器
疾风2002 2003-01-02
  • 打赏
  • 举报
回复
有必要用到正则表达式吗??
直接检测<p>和</p>不就行了??
pqds 2003-01-02
  • 打赏
  • 举报
回复
gz

62,614

社区成员

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

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