0

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
Community
  • 1
  • 1
newbie
  • 929
  • 17
  • 35

4 Answers4

5

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();
        }
    }
}
sdc
  • 2,603
  • 1
  • 27
  • 40
1

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++;
}
ArcticLord
  • 3,999
  • 3
  • 27
  • 47
1

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.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • how to not matching line by line ? it will get long time if i have tons of text. – newbie Jan 15 '16 at 08:30
  • 2
    @newbie You want to print the line number so you will have to go through each line, you can't know in advance if a line matches or not. – Tunaki Jan 15 '16 at 08:31
1

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();
            }
        }
Anand Pandey
  • 383
  • 4
  • 12