• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Matcher.find() ?

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks for the link!
That really explains it all!


Yep, sorry for the poor explanation I gave
 
reply
    Bookmark Topic Watch Topic
  • New Topic