Java正则表达式 - 从匹配的文本中获取行号

时间:2016-01-15 08:09:40

标签: java regex

它来自我的previous question

对于我的情况,我想从正则表达式模式获得行数。例如:

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

我希望从与[0-9]+匹配的文本中获取行数。我希望输出像这样:

line 2
line 3
line 4

4 个答案:

答案 0 :(得分: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();
        }
    }
}

答案 1 :(得分:1)

使用Scanner迭代输入的所有行。并使用Matcher Object检查RegEx Pattern。

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++;
}

答案 2 :(得分: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]+.*"匹配,意味着该行至少包含一个数字,则会打印行号。

答案 3 :(得分:1)

使用匹配器对象检查RegEx模式。

    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();
            }
        }