提取<p id='ttt'>和</p>中间的字符串。
正则表达式怎么写?
例子String str1="OKOK havae rest<p id='ttt'><input type='radio' name='ddd' />ddd</p>adao dkaoe <p id='ttt'><input type='radio' name='ddd' />ddd</p>";
怎样得到<p id='ttt'>和</p>中间的字符串?
正则表达式怎么写?
例子String str1="OKOK havae rest<p id='ttt'><input type='radio' name='ddd' />ddd</p>adao dkaoe <p id='ttt'><input type='radio' name='ddd' />ddd</p>";
怎样得到<p id='ttt'>和</p>中间的字符串?
解决方案 »
- BufferedInputStream读取http图片丢失数据问题
- Hash HashMap
- 问个问题
- 请给个方案,谢谢
- 关于CORBA,命名服务,和WEB NMS 高分急求答案
- 求算法
- 在java里如何把String数据类型转化为int型呢?
- javaw和java命令有什么区别?
- 打印问题
- 新手请教:哪位帮忙看一下这段怎么错了,我明明加了import java.lang.*;为什么提示Undefined variable or class name :Integer和Class StringTokenizer not found
- 在applet中在一个矩形中画圆?(我是新手)
- Java正则表达式...........大家帮忙看看....
String str1 = "OKOK havae rest <p id='ttt'> <input type='radio' name='ddd' />ddd </p>adao dkaoe <p id='ttt'> <input type='radio' name='ddd' />ddd </p>";
Pattern p = Pattern.compile("<p.*?>(.*?)</p>");
Matcher m = p.matcher(str1);
while (m.find()) {
System.out.println(m.group());
}
lz,试试吧,用replaceAll可以把<p id='ttt'>和 </p>替换掉
我尝试了一下syzhwh的方法
发现部分可以实现。主要是<p></p>这两个标记里面的也给我提取出来了。这个里面的东西是我不要的。怎样才能精确到只有<p id='ttt'></p>这两个标记里面的才提取呢?
谢谢大家哦~
对于正则完全不懂~~
:(
<input type='radio' name='ddd' />ddd和<input type='radio' name='ddd' />ddd ?
public static void main(String[] args) {
String str1 = "OKOK havae rest "
+ "<p id='ttt'> <input type='radio' name='ddd' />ddd </p>adao dkaoe "
+ "<p id='ttt'> <input type='radio' name='ddd' />ddd </p>"; String regex = "<p[^>]*>.*?<[//]p>";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str1);
while(m.find()){
String temp = m.group();
System.out.println(temp.substring(temp.indexOf('>') + 1, temp.lastIndexOf("</p>")));
}
}
回复kString,谢谢。我会尝试一下你说的IceWee,
回复No_End_Point,跟syzhwh的一样<p></p>的也出现了~~~
String reg = "<p id='ttt'>(.*?)</p>";Pattern p = Pattern.compile(reg );
Matcher m = p.matcher(str);
while (m.find()) {
System.out.println(m.group(1));
}
group中加数字表示第几个捕获组
System.out.println(m.group().replaceAll("<p.*?>", "").replaceAll("</p>", ""));
while(m.find()){
String temp = m.group();
System.out.println(temp.substring(temp.indexOf('>') + 1, temp.lastIndexOf("</p>")));
}temp.substring(temp.indexOf('>') + 1, temp.lastIndexOf("</p>"))用了个笨办法
但是我不要的<p></p>还是出现了~~~
我也不知道要怎么解决了~~谢谢你们。我比较笨
让你们花了这么长时间。很抱歉啊~~
然后说用正则来做,正则不懂。
代码就是上面的朋友贴出来的那些。。
String context=request.getParameter("content");
String reg= "<p[^>]*>.*?<[//]p>";
Pattern p = Pattern.compile(reg );
Matcher m = p.matcher(context);
while (m.find()) {
System.out.println(m.group(1));
}
context是接受页面fckeditor传送过来的html代码
然后要把这个里面有<p id='ttt'> </p>这个的拆分出来
attention-ing
public static void main(String args[]){
String str1="OKOK havae rest <p id='ttt'> <input type='radio' name='ddd' />ddd </p>adao dkaoe <p id='ttt'> <input type='radio' name='ddd' />ddd </p>";
String regex = "<p\\s*id='.*'>.*</p>";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str1);
while(matcher.find())
System.out.println(matcher.group());
}
}<p id='ttt'> <input type='radio' name='ddd' />ddd </p>adao dkaoe <p id='ttt'> <input type='radio' name='ddd' />ddd </p>我的这个太土了...还要继续替换...感觉要用非匹配的办法...没时间学啊最近忙啊...555
System.out.println(str1.substring(28,66));
结果为:<input type='radio' name='ddd' />ddd
你不是提取 <p id='ttt'>和 </p>中间的字符串吗, 14#的程序可以解决你的问题吧?
package Test;
import java.util.regex.*;
public class qdb
{
public static void f(String str,String regex)
{
Pattern p=Pattern.compile(regex);
Matcher m=p.matcher(str);
while(m.find())
{
System.out.println(m.group(1));
}
}
public static void main(String[] args)
{
String str1="OKOK havae rest <p id='ttt'> <input type='radio' name='ddd' />ddd </p>" +
"adao dkaoe <p id='ttt'> <input type='radio' name='ddd' />ddd </p>";
String regex="(?:<p.*?>)(.*?)(?:</p>)";
f(str1,regex);
}
}
<p class='gf' style="dsfas">等一系列字符串,
(.*?) 捕获组1,用来捕获<p></p>中间的字符串,
(?:</p>)非捕获组,匹配</p>
public static void main(String[] args) {
String str1="OKOK havae rest<p id='ttt'> <input type='radio' name='ddd' />ddd </p>adao dkaoe <p id='ttt'> <input type='radio' name='ddd' />ddd </p>";
String regex = "(?s)(<p.*?p>).*?";
Matcher m = Pattern.compile(regex).matcher("");
m = m.reset(str1);
while(m.find()){
System.out.println("sss"+m.group());
}
}
不过还是要谢谢大家~~以后好好学学正则。蛮重要的~~~:)
不过还是要谢谢大家~~以后好好学学正则。蛮重要的~~~:)