首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

求教正则表达式的有关问题

2012-01-14 
求教正则表达式的问题public class zhengzhe4 {public static void main(String[] args) throws IOExcepti

求教正则表达式的问题
public class zhengzhe4 {

public static void main(String[] args) throws IOException
{ String str = null;
File a = new File("f:/add3.txt");
if(a.exists()){
  FileInputStream fi = new FileInputStream(a);
  InputStreamReader isr = new InputStreamReader(fi, "GBk");
  BufferedReader bfin = new BufferedReader(isr);
   
  String rLine = "";
while((rLine = bfin.readLine())!=null){
  str = str + rLine;
  }
}
if(str!=null && str.length()>0)
{
  Pattern p = Pattern.compile("<title>.*?</title>");

  Matcher m = p.matcher(str);
  while(m.find()){
System.out.println(m.group());


}
if(str!=null && str.length()>0)
{
  Pattern p1 = Pattern.compile("<!-- 正文内容 begin -->.*?<!-- 分享 begin -->");

  Matcher m1 = p1.matcher(str);
  while(m1.find()){
System.out.println(m1.group());


}
if(str!=null && str.length()>0)
{
  Pattern p1 = Pattern.compile("<span id=\"pub_date\">.*?</span>&nbsp;&nbsp");

  Matcher m1 = p1.matcher(str);
  while(m1.find()){
System.out.println(m1.group());


}
}
}


这个是代码  
我拿正则表达式 截取ADD3.TXT里的 字符串
然后打印结果
<title>神八将进行5次轨道控制 3日凌晨与天宫首次对接_新闻中心_新浪网</title>\
<span id="pub_date">2011年11月01日07:51</span>&nbsp;&nbsp


想问问如何 把 <title> </title> 累似这些的前缀去掉 
要怎么写 
求教

[解决办法]

Java code
String d = "321<title>神八</title>3414";Pattern pb = Pattern.compile("<title>.*?</title>");Matcher md = pb.matcher(d);if(md.find()){        String mdg = md.group();        System.out.println("mdg="+mdg);        System.out.println("d="+d.replaceAll("<title>.*?</title>",mdg.substring(7, mdg.length()-8)));} 

热点排行