• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Regex

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.regex.*;
class Regex2 {
public static void main(String[] args) {
Pattern p = Pattern.compile(args[0]);
Matcher m = p.matcher(args[1]);
boolean b = false;
while(b = m.find()) {
System.out.print(m.start() + m.group());
}
}
}

And the command line:
java Regex2 "\d*" ab34ef
What is the result?

------
The above code has been discussed here in length. But the thing I don't have a clue still is why the compiler search for index 6 because its been asked to search the string of length only 6(that means it should stop searching at index 5). Can someone enlighten me?
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Jibs,

have a look at this:
https://coderanch.com/t/263832/java-programmer-SCJP/certification/Regex

Yours,
Bu.
 
jibs parap
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Burk,
I did read other discussions too on this. The penny doesn't drop though re: why the compiler searches index 6. I understand everything except this logic. Sorry mate.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy ranchers!


jibs asked:

why the compiler searches index 6?




Because of the greedy quantifier * you will get also all the zero length matches with /d*
There are zero length matches at the beginning, in the middle but also at the end of the string.

Your string has the following indices:



Note, that the six is also a valid index of your string. For example
System.out.println("ab34ef".substring(3,6));
prints out 4ef.

Your test class will find the zero length substring at the end as well. It is the same as
"ab34ef".substring(6,6). It will find it because the empty string "" at the end matches the pattern: zero or more occurences of a digit /d*
Therefore the 6.


Yours,
Bu.
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This very question is also extensively discussed in the SCJP FAQ.

Burkhard, maybe you want to add this image to the FAQ instead of the ASCII image that's there now.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, did so.
Bu.
 
jibs parap
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Burk.
- I have been always (self)taught the indices look at the single characters rather than the empty spaces between them. Also if the length of a string is x it will have x-1 indices.
I think many ranchers had the same understanding beause that may be the reason why this topic has been discussed in length many times here.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jibs wrote:

I think many ranchers had the same understanding


Yes, and even in the tutorial from Sun they show a figure where the last index is missing.

Have a look:
http://java.sun.com/docs/books/tutorial/essential/regex/literals.html

OK, the end index is in the text, but not in the figure.

Bu.
[ July 28, 2007: Message edited by: Burkhard Hassel ]
 
jibs parap
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True..Cheers
 
Where does a nanny get ground to air missles? Protect this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic