aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes find() method in Regex 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 "find() method in Regex" Watch "find() method in Regex" New topic
Author

find() method in Regex

Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 247



command line is- "\w*" ab23

output-
0 ab23
4


I estimated the output as-
0 ab23

My doubt is why find() method is searching upto index 4? end of string is at index 3, xo why find() goes one index beyond the length?


Astha - OCPJP 6 (90%)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

Astha Sharma wrote:

command line is- "\w*" ab23

output-
0 ab23
4


I estimated the output as-
0 ab23

My doubt is why find() method is searching upto index 4? end of string is at index 3, xo why find() goes one index beyond the length?


The pattern is capable of matching a zero-length string. And you can have a zero length string, at the end of the input string.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Matheus Souza
Ranch Hand

Joined: Mar 06, 2012
Posts: 38

Henry Wong wrote:
Astha Sharma wrote:

command line is- "\w*" ab23

output-
0 ab23
4


I estimated the output as-
0 ab23

My doubt is why find() method is searching upto index 4? end of string is at index 3, xo why find() goes one index beyond the length?


The pattern is capable of matching a zero-length string. And you can have a zero length string, at the end of the input string.

Henry


Complementing what Henry said:
Just put aroung the m.group(), these characters ">" and "<". You'll see that the quantificator '*' prints an empty string too.
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1179
Matheus Souza wrote:Just put aroung the m.group(), these characters ">" and "<". You'll see that the quantificator '*' prints an empty string too.


Right, it prints now -



Regards,
Dan


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 247

thanks to all for replying. I understand that pattern will search for a zero length string also. But i'm not getting that why it is continuing search beyond the matcher string. Matcher string doesn't have index upto 4.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

Astha Sharma wrote:thanks to all for replying. I understand that pattern will search for a zero length string also. But i'm not getting that why it is continuing search beyond the matcher string. Matcher string doesn't have index upto 4.



Regular expressions searches for things other than text strings -- including word boundaries, end of input boundaries, etc. As such, the position at the end of text is a valid position.

Now, a position such as 5 (greater than the length) is beyond the string.

Henry
Matheus Souza
Ranch Hand

Joined: Mar 06, 2012
Posts: 38

Astha Sharma wrote:thanks to all for replying. I understand that pattern will search for a zero length string also. But i'm not getting that why it is continuing search beyond the matcher string. Matcher string doesn't have index upto 4.


The quantificator '*' search for zero, one or many correspondences, so the empty string in the end kind of represents the final of the string and its an correspondence (of 0)
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1179
We can look at -



versus -



Regards,
Dan

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: find() method in Regex