my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regex: find() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regex: find() method" Watch "Regex: find() method" New topic
Author

Regex: find() method

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

Straight from the K&B 310-055 page 483

The important method in this program is find() method. This is the method that
actually cranks up the regex engine and does some searching. The find() method
returns true if it gets a match, and remembers the start position of the match. If find() returns true, you can call the start() method to get the starting position of the match, and can call the group() method to get the string that represents the actual bit of source of data that was matched[/b]



My question is, if find() method returns false, (no match in the round), why doesn't loop comes out.
What is special there?
Actually I am not getting how this find works.
Please help!!!


Regards,
cmbhatt


cmbhatt
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Chandra Bhatt:
...My question is, if find() method returns false, (no match in the round), why doesn't loop comes out...

Sorry, I don't have the book. Is there some code to go with this?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Tommaso Nuccio
Ranch Hand

Joined: Dec 11, 2006
Posts: 66
That's the code:


I think that is because of the assignment in the while-loop.
It will do "m.find()" until the end of the matching string is reached.

The Java Specs says:

This method starts at the beginning of the input sequence or, if a previous invocation of the method was successful and the matcher has not since been reset, at the first character not matched by the previous match.


Anyway, good question.


Ciao,<br /> Tommaso<br /> <br />~*~*~*~<br />There are 10 types of people, those who understand binary and those who don't.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Thanks Tommaso,

Does this mean, while loop has no part in that,

doesn't find() method return false if no match found for that iteration on a String sequence. If it returns false, while loop should terminate.

I am not able to fix the bolt of my brain on this issue!!!

Marc, Tommaso has provided the code!!!

Please help!!!


Regards,
cmbhatt
Jesse Custer
Ranch Hand

Joined: Feb 07, 2007
Posts: 45
The find() method returns false after no more patterns are found in the matched String. So only after the whole String is checked.
The while loop in the code has nothing to do with checking the String (That is done within the find() method). It's only purpose is to iterate over the found matches.
sravanthi pulukuri
Ranch Hand

Joined: Mar 15, 2007
Posts: 125
Hai chandra,
Can you please explain me also i couldnot understand solution given
Thanks
Tommaso Nuccio
Ranch Hand

Joined: Dec 11, 2006
Posts: 66
sravanthi,

the find() method goes on until the end of the string to check. This happens when it reached the end of the string.
Thus m.find() returns false only at the end of the string, because there can't be no more matchings.
Then the while loop breaks, because b == false.

...and all that remains is an output.
Isn't that sad

Regards.
sravanthi pulukuri
Ranch Hand

Joined: Mar 15, 2007
Posts: 125
thanks now i followed,so it does not depend on the loop,untill entire string is searched right??
thanks
Jesse Custer
Ranch Hand

Joined: Feb 07, 2007
Posts: 45
If you are still unclear about this then check out the next piece of code. It's a simplified version of the Matcher and Pattern class.


[ April 17, 2007: Message edited by: Jesse Custer ]
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Thanks Tommaso and Custer,

But what is written in the book K&B,
"... you can call the start() method when find() returns true."

It is really confusing,
I agree Tomasso what you say:
"the find() method goes on until the end of the string to check."
"I agree because it is really what we see"

But when is the call to start() done, what K&B says when find()
returns true.

How is it determined that find() returns true, the loop is
misguiding me! We can't cram this to the brain that it happens. It is
big question until we get the satisfactory answer.


Regards,
cmbhatt
Jesse Custer
Ranch Hand

Joined: Feb 07, 2007
Posts: 45
I don't follow anymore where you have problem understanding. I think the code example I provided has a logical flow similar to the java.util.regex classes. So if you understand that, then I don't see the problem.

The find() method will search the String until it finds a match. Then it stops searching and returns true. From then on we can call the start() method to get the starting index of the match.
If we call the find() method again, it will continue searching from where it stopped until it finds another match or until it reaches the end of the String.
If it reaches the end of the String without finding a match, it will return false and hence you can not call the start() method anymore because there is no match and thus no starting index.
[ April 17, 2007: Message edited by: Jesse Custer ]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Given this code

,

the only way for the body of the loop to be executed is if the condition of the while loop is true.

This requires that find() return true.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Yeah Custer,

From your latest description I got it!
Go on go on searching and return true when you find, now we can call start(),
group() etc. (if any ). OK! Then find() will start from the position it
left and again go on, go on until you find true, if true we can call start(),
and again the same process. When it returns false, OUT OF LOOP ofcourse.


Is that correct???
If I am correct, I understood your program too! Otherwise



Regards,
cmbhatt
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

and what happens, when find() returns false and you access the group() method then?

Try this (not very sensible but perhaps enlighting) code with/without the outcommented line:


Good Luck,
Bu.


all events occur in real time
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Thanks Burkhard,

This example was really eye opening and polished the previous answers, What I got from it, find() only returns true when find() matches; otherwise continues
with its internal searching (what it does); and only once returns false when it completes the search with whole string; consequently we come out out loop.
(never blame to while loop, it is perfect in its place; what we guessed before
the strange behavior of while(), but it was false).

What I got from the example you provided, find only returned true two times obviously when it found the 'X'. Otherwise false once, and because of the for loop we got subsequent output of false till the for() loop terminates. But when we uncommented the group(), we got IllegalStateException; GOOD;
what could group() have returned!!!

If my above writing goes true, alright for me! otherwise
Great effort anyway by Burkhard;



Regards,
cmbhatt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regex: find() method
 
Similar Threads
Problem with FileWrite and Regular Expression
Regex: Need clarification on two issues
While loop question from SCJP 5.0
doubt on group() in Matcher class
java.util.regex