2016-01-15 51 views
0

它基於我的previous questionJava正則表達式 - 從匹配文本中獲取行號

對於我的情況,我想從正則表達式模式獲得行數。 E.g:

name : andy 
birth : jakarta, 1 jan 1990 
number id : 01011990 01 
age : 26 
study : Informatics engineering 

我想從文本獲得行的數量匹配號碼[0-9]+的。我想輸出是這樣的:

line 2 
line 3 
line 4 
+0

你能指定你的問題嗎?你在說什麼?爲什麼你會使用RegEx? – Alex

+0

包含數字(十進制)的行。你有最好的方法嗎? – newbie

+0

如果您想提取所有包含數字的行,請參閱下面的答案。 – Alex

回答

1

使用Scanner迭代您輸入的所有行。並使用Matcher對象來檢查RegEx模式。

String s = "name : andy\n" + 
      "birth : jakarta, 1 jan 1990\n" + 
      "number id : 01011990 01\n" + 
      "age : 26\n" + 
      "study : Informatics engineering"; 

Scanner sc = new Scanner(s); 
int lineNr = 1; 
while (sc.hasNextLine()) { 
    String line = sc.nextLine(); 
    Matcher m = Pattern.compile(".*[0-9].*").matcher(line); 
    if(m.matches()){ 
     System.out.println("line " + lineNr); 
    } 
    lineNr++; 
} 
+0

如何不逐行匹配?如果我的文字非常多,它會很長時間。 – newbie

1

你可以簡單地具備以下條件:

public static void main(String[] args) throws IOException { 
    int i = 1; 
    Pattern pattern = Pattern.compile(".*[0-9]+.*"); 
    try (BufferedReader br = new BufferedReader(new FileReader("..."))) { 
     String line; 
     while ((line = br.readLine()) != null) { 
      if (pattern.matcher(line).matches()) { 
       System.out.println("line " + i); 
      } 
      i++; 
     } 
    } 
} 

此代碼只是打開一個BufferedReader到它(一個給定的文件路徑和遍歷每一行,直到readLine()回報null,顯示的結束文件)。如果該行與模式".*[0-9]+.*"匹配,意味着該行至少包含一位數字,則會打印行號。

+0

如何不逐行匹配?如果我有很多文字,它會很長時間。 – newbie

+2

@newbie你想要打印行號,所以你將不得不通過每一行,你不能預先知道一行是否匹配。 – Tunaki

2

這將爲你做。我修改了正則表達式".*[0-9].*"

import java.io.IOException; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
import java.util.stream.Stream; 
import java.util.regex.Pattern; 
import java.util.concurrent.atomic.AtomicInteger; 

class RegExLine 
{ 
    public static void main(String[] args) 
    { 
     new RegExLine().run(); 
    } 

    public void run() 
    { 
     String fileName = "C:\\Path\\to\\input\\file.txt"; 
     AtomicInteger atomicInteger = new AtomicInteger(0); 
     try (Stream<String> stream = Files.lines(Paths.get(fileName))) 
     { 
      stream.forEach(s -> 
      { 
       atomicInteger.getAndIncrement(); 
       if(Pattern.matches(".*[0-9].*", s)) 
       { 
        System.out.println("line "+ atomicInteger); 
       } 
      }); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

小,簡單 - 必須是Java 8; =)使用正確的JDK @newbie – Alex

+0

@newbie可以請你接受我的回答,或者澄清你的問題。謝謝。 – sdc

1

使用Matcher對象檢查正則表達式。

public static void main(String[] args) 
     { 
      String s = "name : andy\n" + "birth : jakarta, 1 jan 1990\n" + "number id : 01011990 01\n" + "age : 26\n" 
        + "study : Informatics engineering"; 
      try 
      { 
       Pattern pattern = Pattern.compile(".*[0-9].*"); 
       Matcher matcher = pattern.matcher(s); 
       int line = 1; 
       while (matcher.find()) 
       { 
        line++; 
        System.out.println("line :" + line); 
       } 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 
     }