This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Matcher.find() ?

 
Hans Beck�rus
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is because it will consider the last test after "f" to be empty string. Try this :

 
Hans Beck�rus
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic