File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Matcher.find() ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Matcher.find() ? " Watch "Matcher.find() ? " New topic
Author

Matcher.find() ?

Hans Beck�rus
Greenhorn

Joined: Aug 23, 2006
Posts: 24
I have some issues understanding the logic implemented by the find()/start() methods in class Matcher .



I would have guessed that this code produced the output:
0123445
But it actually gives:
01234456
The last '6' is my concern here, why does it return the
extra '6' in the end? For any output to be printed find()
must return true, how can it return true for a value of
start() being 6? It is outside the pattern string range?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

The problem is because it will consider the last test after "f" to be empty string. Try this :



[My Blog]
All roads lead to JavaRanch
Hans Beck�rus
Greenhorn

Joined: Aug 23, 2006
Posts: 24
Ok, so then my assumption that find() actually
includes the terminating null character in its logic is correct?
That was not what I would have expected from a find()
implementation. A regex on an empty string (only a \0)
I would have assumed returned 'false' since it has nothing to
compare against! Tricky, really tricky! I would certainly
have failed this question on the exam

Thanks.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

find() actually includes the terminating null character in its logic is correct

No. The regex "*" will return true once, on an empty string. The last try after "f" is then considered an empty string, so returning true. It will stop at the null character.

You'll find more about Zero-Length Matches here:
http://java.sun.com/docs/books/tutorial/essential/regex/quant.html
[ September 25, 2006: Message edited by: Satou kurinosuke ]
Hans Beck�rus
Greenhorn

Joined: Aug 23, 2006
Posts: 24
Ok well, I believe an "empty" string is just a \0 in memory with
a reference to it.
If find() returns true on an empty string means it must have
evaluated/included also the \0 as a valid source in its logic.
So empty strings (\0) are handled as a special case.

Thanks for the link!
That really explains it all!

[ September 25, 2006: Message edited by: Hans Beck�rus ]
[ September 25, 2006: Message edited by: Hans Beck�rus ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Thanks for the link!
That really explains it all!

Yep, sorry for the poor explanation I gave
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Matcher.find() ?