Java Reference
In-Depth Information
new
new URL ( url ). openStream ()));
// Read the URL looking for the rank information, as
// a single long string, so can match RE across multi-lines.
final
final String input = readerToString ( is );
// If found, append to sales data file.
Matcher m = r . matcher ( input );
iif ( m . find ()) {
// Paren 1 is the digits (and maybe ','s) that matched; remove comma
return
return Integer . parseInt ( m . group ( 1 ). replace ( "," , "" ));
} else
else {
throw
throw new
new RuntimeException (
"Pattern not matched in `" + url + "'!" );
}
}
private
private static
static String readerToString ( BufferedReader is ) throws
throws IOException {
StringBuilder sb = new
new StringBuilder ();
String line ;
while
while (( line = is . readLine ()) != null
null ) {
sb . append ( line );
}
return
return sb . toString ();
}
}
Program: Full Grep
Now that we've seen how the regular expressions package works, it's time to write JGrep , a
full-blown version of the line-matching program with option parsing. Table 4-2 lists some
typical command-line options that a Unix implementation of grep might include.
Search WWH ::




Custom Search