Using a Regular Expression to Filter Lines from a Reader : Serialization « Regular Expressions « Java






Using a Regular Expression to Filter Lines from a Reader


import java.io.BufferedReader;
import java.io.FileReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
  public static void main(String[] argv) throws Exception {
    String filename = "infile.txt";
    String patternStr = "pattern";
    BufferedReader rd = new BufferedReader(new FileReader(filename));

    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher("\\D");

    String line = null;
    while ((line = rd.readLine()) != null) {
      matcher.reset(line);
      if (matcher.find()) {
        // line matches the pattern
      }
    }
  }
}

 








Related examples in the same category

1.Use FileChannels and ByteBuffers to Store Patterns
2.Reading Lines from a String Using a Regular Expression
3.Apply Regular Expressions on the contents of a file