• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex: find() method

 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Tommaso Nuccio
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai chandra,
Can you please explain me also i couldnot understand solution given
Thanks
 
Tommaso Nuccio
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks now i followed,so it does not depend on the loop,untill entire string is searched right??
thanks
 
Jesse Custer
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic