• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

find() method in Regex

 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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?
 
Henry Wong
author
Marshal
Pie
Posts: 21431
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Matheus Souza
Ranch Hand
Posts: 38
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21431
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can look at -



versus -



Regards,
Dan

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic