Pattern-match lines of a file
Here's an example of grep-like treatment of the contents of a text file.
In this example, the format of each line is verified against a regular expression.
Note that a String literal representing a regular expression needs to escape all backslashes. In effect, all of the backslashes are simply doubled.
Example
import java.io.*; import java.util.regex.*; public class LineMatcher { /** * Verifies that each line of a text file starts with: * \N + tab + integer + tab + text * where "text" contains word characters (\w). * * The corresponding regular expression is: * "^\\N\t(\d)+\t(\w)+" * * The String passed to Pattern needs to double every backslash: * "^\\\\N\\t(\\d)+\\t(\\w)+" * * If a line is not of the expected pattern, then an * IllegalStateException is thrown. */ public static void findBadLines( final File aFile ) { //...checks on aFile are elided //Pattern and Matcher are used here, not String.matches(regexp), //since String.matches(regexp) would repeatedly compile the same //regular expression Pattern regexp = Pattern.compile("^\\\\N\\t(\\d)+\\t(\\w)+"); Matcher matcher = regexp.matcher(""); LineNumberReader lineReader = null; try { lineReader = new LineNumberReader( new FileReader(aFile) ); String line = null; while ((line = lineReader.readLine()) != null){ matcher.reset( line ); //reset the input if ( !matcher.find() ) { String msg = "Line " + lineReader.getLineNumber() + " is bad: " + line; throw new IllegalStateException(msg); } } } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex){ ex.printStackTrace(); } finally { try { if (lineReader!= null) lineReader.close(); } catch (IOException ex) { ex.printStackTrace(); } } } /** * Test harness. */ public static void main (String... arguments) { final File file = new File( "C:\\Temp\\RegexpTest.txt" ); LineMatcher.findBadLines( file ); } }
See Also :
Would you use this technique?
|
|