newbie
newbie

Reputation: 959

Java regex - get line number from matching text

It's based from my previous question.

For my case I want to get number of line from regex pattern. E.g :

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

I want to get number of line from text that match of number [0-9]+. I wish output like this :

line 2
line 3
line 4

Upvotes: 0

Views: 6019

Answers (4)

Anand Pandey
Anand Pandey

Reputation: 393

Use Matcher Object to check for RegEx Pattern.

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

Upvotes: 1

sdc
sdc

Reputation: 3021

This will do it for you. I modified the regular expression to ".*[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();
        }
    }
}

Upvotes: 5

Tunaki
Tunaki

Reputation: 137064

You could simply have the following:

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

This code simply opens a BufferedReader to a given file path and iterates over each line in it (until readLine() returns null, indicating the end of the file). If the line matches the pattern ".*[0-9]+.*", meaning the line contains at least a digit, the line number is printed.

Upvotes: 1

ArcticLord
ArcticLord

Reputation: 4039

Use a Scanner to iterate all lines of your input. And use Matcher Object to check for 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++;
}

Upvotes: 1

Related Questions