Skip to content

Instantly share code, notes, and snippets.

@sixty4bit
Created March 12, 2011 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sixty4bit/867598 to your computer and use it in GitHub Desktop.
Save sixty4bit/867598 to your computer and use it in GitHub Desktop.
Iterative Improvement
// Make all lines comments first
BufferedReader inputFile = new BufferedReader(new FileReader("input.txt"));
String line = null;
while((line=inputFile.readLine())!=null){
System.out.println("comment found: " + line);
}
// Now find email addresses
BufferedReader inputFile = new BufferedReader(new FileReader("input.txt"));
String line = null;
String emailPattern = "([a-zA-Z_0-9\\.]+@email\\.com)?";
while((line=inputFile.readLine())!=null){
if(Pattern.matches(emailPattern, line)) { System.out.println("email found: " + line); }
else { System.out.println("comment found: " + line); }
}
// Now find regular addresses, notice I am just checking for the zip code...
BufferedReader inputFile = new BufferedReader(new FileReader("input.txt"));
String line = null;
String emailPattern = "([a-zA-Z_0-9\\.]+@email\\.com)?";
String addressPattern = "\\d\\d\\d\\d\\d$?";
while((line=inputFile.readLine())!=null){
if(Pattern.matches(emailPattern, line)) { System.out.println("email found: " + line); }
else if(Pattern.matches(addressPattern, line)) {
System.out.println("address found: " + line);
outputFormattedAddress(line);
}
else { System.out.println("comment found: " + line); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment