Re: help using pattern and matcher classes
IchBin wrote:
Petterson Mikael wrote:
Hi,
I have two words that I need to search my files for.
DONUMBER and DO_NUMBER.
I am not sure how I can create two patterns to search for in the same
file. Also what is the difference between the pattern and matcher
(since you can use regex in both).
This is the code I will use:
try {
// Create the reader
String filename = "infile.txt";
String patternStr1 = "DONUMBER";
String patternStr2 = "DO_NUMBER";
BufferedReader rd = new BufferedReader(new
FileReader(filename));
// Create the pattern
Pattern pattern1 = Pattern.compile(patternStr1);
Pattern pattern2 = Pattern.compile(patternStr2);
Matcher matcher1 = pattern1.matcher("");
Matcher matcher2 = pattern2.matcher("");
// Retrieve all lines that match pattern
String line = null;
while ((line = rd.readLine()) != null)
{
matcher2.reset(line);
matcher1.reset(line);
if (matcher2.find() || matcher1.find())
// line matches the pattern
}
}
} catch (IOException e) {
}
Also take a look at this "Implementing a FilterReader to Filter Lines
Based on a Regular Expression" in the The Java Developers Almanac
http://javaalmanac.com/egs/java.util.regex/LineFilter2.html
Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)