import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class test1_20 {
public static String ReadData(){ 
  try { 
   FileReader read = new FileReader("D:/data.txt"); 
   BufferedReader br = new BufferedReader(read); 
   String row; 
   while((row = br.readLine())!=null){ 
    System.out.println(row); 
   } 
  } catch (FileNotFoundException e) { 
   e.printStackTrace(); 
  } catch (IOException e){ 
   e.printStackTrace(); 
  }
return null; 

public static void main(String[] args) { Pattern p;
Matcher m;
String title=ReadData(); p=Pattern.compile("<title>.*?</title>");
m=p.matcher(title);
while(m.find()){
String str=m.group();
System.out.println("从"+m.start()+"到"+m.end()+"匹配模式子序列");
System.out.println(str);
}



}
}

解决方案 »

  1.   

    都return null了,能匹配到才怪
      

  2.   

    这个该怎么做啊,我需要吧txt文档中的<title></title>之间的文字提取出来,改怎么做啊
      

  3.   

    全部读到一个StringBuilder中,最后返回它的字符串,用title接收
      

  4.   


            import java.io.BufferedReader;  
    import java.io.File;  
    import java.io.FileNotFoundException;  
    import java.io.FileReader;  
    import java.io.IOException;  
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class test1_20 {
    public static String ReadData(){  
    try {  
    FileReader read = new FileReader("D:/data.txt");  
    BufferedReader br = new BufferedReader(read);  
    String row,result;
    while((row = br.readLine())!=null){  
    System.out.println(row);  
    result += row;
    }  
    } catch (FileNotFoundException e) {  
    e.printStackTrace();  
    } catch (IOException e){  
    e.printStackTrace();  
    }
    return result;  
    }  
    public static void main(String[] args) {Pattern p;
    Matcher m;
    String title=ReadData();p=Pattern.compile("<title>.*?</title>");
    m=p.matcher(title);
    while(m.find()){
    String str=m.group();
    System.out.println("从"+m.start()+"到"+m.end()+"匹配模式子序列");
    System.out.println(str);
    }
      

  5.   


    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class Test1_20
    {
    public static String ReadData()
    {
    FileReader read = null;
    BufferedReader br = null;
    Pattern p = Pattern.compile("(?<=<title>)(.*?)(?=</title>)");
    Matcher m;
    String row = null;
    try {
    read = new FileReader("D:/data.txt");
    br = new BufferedReader(read); while ((row = br.readLine()) != null) {
    m = p.matcher(row);
    if (m.find()) {
    row = m.group(1);
    br.close();
    read.close();
    return row;
    }
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (br != null) {
    br = null;
    }
    if (read != null) {
    read = null;
    }
    }
    return null;
    } public static void main(String[] args)
    {
    String title = ReadData(); if (null != title) {
    System.out.println("The title is :" + title);
    } }
    }