This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams and have Darcy DeClute on-line!
See this thread for details.
  • 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

regex in SCJP test

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,
I am trying to solve an scjp test about regex.

here is a code...

import java.util.regex.*;

public class TestRegex{
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
java TestRegex "\d*" ab34ef
the answer is 01234456. I understood everything except the last output(6). Since the last index in "ab34ef" is 5, how is it possible to be printed 6 ?

Any help ....


 
Ranch Hand
Posts: 94
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

tural ferhadov wrote:hello,
I am trying to solve an scjp test about regex.

here is a code...

import java.util.regex.*;

public class TestRegex{
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
java TestRegex "\d*" ab34ef
the answer is 01234456. I understood everything except the last output(6). Since the last index in "ab34ef" is 5, how is it possible to be printed 6 ?

Any help ....




Here is my try:
The expression "\d*" refers to any digit of length 0 or more; thus, it will match to all zero size matches too. The first match start before 'a' and end before 'a' (size zero match); this will print 0. What it means is the last match would be after 'f' (size zero match) and thus it will result in 6.
 
Greenhorn
Posts: 9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at this - I found this really useful when I was confused about the same thing.

Good luck!
 
tural ferhadov
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys, it really helped
 
Let nothing stop you! Not even this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic