2015-04-05 49 views
3

代碼:用java正則表達式讀取文本文件來匹配多個模式,我想

import java.io.*; 
import java.util.regex.*; 
public class All { 
    public static void main(String[] args) { 
     String input = "IT&&faculty.*"; 
     try { 
      FileInputStream fstream = new FileInputStream("uu.txt"); 
      DataInputStream in = new DataInputStream(fstream); 
      BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
      String strLine; 
      while ((strLine = br.readLine()) != null) { 
       if (Pattern.matches(input, strLine)) { 
        Pattern p = Pattern.compile("'(.*?)'"); 
        Matcher m = p.matcher(strLine); 
        while (m.find()) { 
         String b = m.group(1); 
         String c = b.toString() + ".*"; 
         System.out.println(b); 

         if (Pattern.matches(c, strLine)) { 
          Pattern pat = Pattern.compile("<(.*?)>"); 
          Matcher mat = pat.matcher(strLine); 
          while (mat.find()) { 
           System.out.println(m.group(1)); 

          } 
         } else { 
          System.out.println("Not found"); 
         } 
        } 
       } 
      } 
     } catch (Exception e) { 
      System.err.println("Error: " + e.getMessage()); 
     } 
    } 
} 

我的文本文件的內容是: \表示它是一個換行符

輸入文件:

IT&&faculty('Mousum handique'|'Abhijit biswas'|'Arnab paul'|'Bhagaban swain') 
Mousum handique(designation|address|phone number|'IT Assistant   professor'|<AUS staff quaters>|#5566778899#) 
Abhijit biswas(designation|address|phone number|'IT Assistant professor'|<AUW staff quaters>|#5566778891#) 
Arnab paul(designation|address|phone number|'IT Assistant professor'|<AUE staff quaters>|#5566778890#) 
Bhagaban swain(designation|address|phone number|'IT Assistant professor'|<AUW staff quarters>|#5566778892#) 

它給出的結果 -

Mousum handique 
Not found 
Abhijit Biswas 
Not found 
Arnab Paul 
Not found 
Bhagaban swain 
Not found 

而我想要的結果是:

Mousum handique 
AUS staff quaters 
Abhijit Biswas 
AUW staff quaters 
Arnab Paul 
AUE staff quaters 
Bhagaban swain 
AUW staff quaters 

這是第一次比賽後,我想,當它得到Mousu​​m handique從文件應該再次搜索該文件,並在那裏得到這樣一行Mousu​​m handique它應該打印任何內<>對應的行。請參閱我的文本文件的數據來理解我的問題。對不起,如果我的問題似乎很愚蠢,但我嘗試了很多!

回答

4

你並不需要使用string.matches方法只使用Patttern和匹配器類在同一行本身<>之間提取這是在該行的開頭名稱,也是內容。

String s = "IT&&faculty('Mousum handique'|'Abhijit biswas'|'Arnab paul'|'Bhagaban swain')\n" + 
     " Mousum handique(designation|address|phone number|'IT Assistant   professor'|<AUS staff quaters>|#5566778899#)\n" + 
     " Abhijit biswas(designation|address|phone number|'IT Assistant professor'|<AUW staff quaters>|#5566778891#)\n" + 
     "Arnab paul(designation|address|phone number|'IT Assistant professor'|<AUE staff quaters>|#5566778890#)\n" + 
     "Bhagaban swain(designation|address|phone number|'IT Assistant professor'|<AUW staff quarters>|#5566778892#)"; 
Matcher m = Pattern.compile("(?m)^\\s*([^\\(]+)\\([^\\)]*\\|<([^>]*)>[^\\)]*\\)").matcher(s); 
while(m.find()) 
{ 
    System.out.println(m.group(1)); 
    System.out.println(m.group(2)); 
} 

輸出:

Mousum handique 
AUS staff quaters 
Abhijit biswas 
AUW staff quaters 
Arnab paul 
AUE staff quaters 
Bhagaban swain 
AUW staff quarters 

DEMO

更新:

使用此正則表達式也獲得ID號。

String s = "IT&&faculty('Mousum handique'|'Abhijit biswas'|'Arnab 
paul'|'Bhagaban swain')\n" + 
       " Mousum handique(designation|address|phone number|'IT Assistant   professor'|<AUS staff quaters>|#5566778899#)\n" + 
       " Abhijit biswas(designation|address|phone number|'IT Assistant professor'|<AUW staff quaters>|#5566778891#)\n" + 
       "Arnab paul(designation|address|phone number|'IT Assistant professor'|<AUE staff quaters>|#5566778890#)\n" + 
       "Bhagaban swain(designation|address|phone number|'IT Assistant professor'|<AUW staff quarters>|#5566778892#)"; 
     Matcher m = Pattern.compile("(?m)^\\s*([^\\(]+)\\([^\\)]*\\|<([^>]*)>[^\\)]*\\|#([^#]*)#[^\\)]*\\)").matcher(s); 
     while(m.find()) 
     { 
      System.out.println(m.group(1)); 
      System.out.println(m.group(2)); 
      System.out.println(m.group(3)); 
     } 

輸出:

Mousum handique 
AUS staff quaters 
5566778899 
Abhijit biswas 
AUW staff quaters 
5566778891 
Arnab paul 
AUE staff quaters 
5566778890 
Bhagaban swain 
AUW staff quarters 
5566778892 
+0

Thans幫助,但我不能把數據作爲一個字符串我有把它放在一個文本文件中,並使用正則表達式從那裏檢索數據。我剛剛給了一個實例,我需要答案在匹配相應的名稱後<..>,但有時在匹配名稱後,我可能還需要#..#中的一個。對不起,但plz幫助如何使用文本文件只 – 2015-04-05 06:31:04

+0

檢查我的更新.. – 2015-04-05 06:40:41

+0

問題是我不能strore字符串的數據就像在字符串中我要使用文本文件讀取它,然後提取數據在第一如果它找到了名字,則匹配,然後在下一個匹配中找到相應的名稱,它只能從文本文件中打印<...>或#..#。 Plz help – 2015-04-05 07:42:01

1

一個錯誤是在這裏:

while (mat.find()) { 
    System.out.println(m.group(1)); // <-- you should use mat - not m!!! 
} 

第二個錯誤是在這裏:

if (Pattern.matches(c, strLine)) { 

if爲n自從字符串c是上一場比賽+「.*」以來輸入。刪除這個條件,它會工作。

固定碼:

... 
    Pattern p = Pattern.compile("'(.*?)'"); 
    Matcher m = p.matcher(strLine); 
    while (m.find()) { 
     String b = m.group(1); 
     System.out.println(b);    
     Pattern pat = Pattern.compile("<(.*?)>"); 
     Matcher mat = pat.matcher(strLine); 
     while (mat.find()) { 
      System.out.println(mat.group(1)); 

     }    
    } 
    ... 

運行此代碼與輸入:

"Abhijit biswas(designation|address|phone number|'IT Assistant professor'|<AUW staff quaters>|#5566778891#) 

輸出:

IT Assistant professor 
AUW staff quaters 
+0

這隻會導致姓名,並沒有給出< >每個對應的名字 – 2015-04-05 07:39:04

+0

@SoumyasreeBiswas我運行的例子是:「」Abhijit biswas(指定|地址|電話號碼|'IT助理教授'| |#5566778891#)「。看到我剛剛添加到答案的歷史部分。 – alfasin 2015-04-05 15:08:25

+0

我得到它解決了很久回來仍然thanx幫助! – 2015-04-05 15:28:56