• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Pattern & Matcher

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whoa !!! I am sooo confused regarding this subject.

Question is:

public static void main(String[] a) {
Pattern p = Pattern.compile("\\d*");
Matcher m = p.matcher("ab34ef");
boolean b = false;
while (b = m.find()) {
System.out.println(m.start() + m.group());
}
}

Answer is 01234456

Can anyone explain THAT to me???

Thanks!!!
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi jennifer

In the code ,
Pattern p = Pattern.compile("\\d*");

you have asked the compiler to locate the data that contains either 0 or more digits.

So, I can split the answer for each data item as

LOC Data
0 -
1 -
2 34
4 -
5 -
6 - (no more)

Hope you get it
 
Jennifer Moran
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
YES !!!

I get it.....it's the ZERO or more that confused me.

Thanks!!

Jennifer
 
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, i will be your nice compiler here is what you get for each iteration

first this pattern will search for zero or more digits since you are using the * your string is "ab34ef"

1. first match is "a" and it can be applied to "zero or more digits" so you get
m.find() = 0
m.group() = empty string
output on the console = 0

2. second match is "b" and it can be applied to "zero or more digits" so you get
m.find() = 1
m.group() = empty string
output on the console = 01

3. third match is "34" and it can be applied to "zero or more digits" so you get
m.find() = 2
m.group() = 34
output on the console = 01234

*note that for the (3) mathch it didn't toke only the "3" but it took the "34" match since the wildcards are "greedy" they will try to mach as much as possible of the string.


4. forth match is "e" and it can be applied to "zero or more digits" so you get
m.find() = 4
m.group() = empty string
output on the console = 012344

5. fith match is "f" and it can be applied to "zero or more digits" so you get
m.find() = 5
m.group() = empty string
output on the console = 0123445

6. finally nothing remains in the string which maches "zero or more digits" so you get
m.find() = 6
m.group() = empty string
output on the console = 01234456


done.


;) hope you got it, its a bit tricky


(peace)



 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The same question has been asked before, here are some of the previous posts:

https://coderanch.com/t/270756/Programmer-Certification-SCJP/Regex-SCJP
https://coderanch.com/t/270569/Programmer-Certification-SCJP/quantifier
https://coderanch.com/t/270015/Programmer-Certification-SCJP/Regex-Self-Test
https://coderanch.com/t/268176/Programmer-Certification-SCJP/K-B-book-chapter-excercienot

Note: Next time when you copy a question from a book or mock exam, quote your sources - that's required on JavaRanch.
 
That is a really big piece of pie for such a 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