This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes Regular Expression Package -- Uncertain If I Did It Right Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
Author

Regular Expression Package -- Uncertain If I Did It Right

JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
I ran a test: to find a piece of text "Bill" in a MS Word document. My simple Java program compiled and ran (java MatchingText). I am 100% certain that there are "two" occurrences of literal String "Bill" in the source document. However, I had only "one" message "A match is found" outputted to the console. I wonder if my code is right.
Here is my code:
import java.util.regex.*;
import java.io.*;
public class MatchingText {
public static void main(String[] args) throws Exception {
File fin = new File("c:/javaprj/letter.doc");
//Open an input stream
FileInputStream fis =
new FileInputStream(fin);
BufferedReader in = new BufferedReader(
new InputStreamReader(fis));
String input = "Bill";
// Create a pattern to match input
Pattern p = Pattern.compile(input);
Matcher m = p.matcher(input);
if (m.find()) {
System.out.println("A match is found"); }
in.close();
}
}
Brian McCallister
Greenhorn

Joined: Dec 03, 2002
Posts: 19
You only should get one match based on that code. The code is basically asking "does this String match this pattern" and the answer is yes, regardless of how many times.
Iterating through the matches would be done along the lines of
while(matcher.find())
{
System.out.println("Found a match");
}
-Brian
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Sorry. I tried your suggestion. Only "one" message was outputted to the console. I wonder how to make the program to output for each occurrence.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Look closely at your code. Right now you're searching the string "Bill" for occurances of the word "Bill". Naturally this occurs exactly once. You need to make use of the BufferedReader you've created to read in a different input string, and search that string for "Bill". You also need to give your variables better names so that you can tell them apart. "Bill" isn't the input - it's what you're searching for. Calling it something like "pattern", "target", or "targetString" woulod help in reminding you (and others) what it's intended role is. The other string(s), the one(s) you read from the BufferedReader, are what should be called "input".
Note that the reason I say "string(s)" is that you have a choice - you can either read the BufferedReader one line at a time and search each line separately (in a loop), or you can read the whole reader contents into a single String (built with StringBuffer or StringWriter) which you then search all at once. E.g.

Now all the stream contents are in one string. (Which may be really big if the file is large enough.) For some applications this approach is best; for others, reading one line at a time makes more sense. Choose whichever you prefer.


"I'm not back." - Bill Harding, Twister
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Thanks, Jim. Now, I understand the Regular Expression package better.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Continued here...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Regular Expression Package -- Uncertain If I Did It Right