This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

find() method in Regex

 
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 38
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We can look at -



versus -



Regards,
Dan

 
You can thank my dental hygienist for my untimely aliveness. So tiny:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic