• 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

Doubt in Regex pattern

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,
Please find the following code and let me know how exactly start and group work in this case and the output shows even 6.

code:
--------------------------------------------------------------------------------

import java.util.regex.*;
class Regex2 {
public static void main(String[] args) {
Pattern p = Pattern.compile("\\d*");
Matcher m = p.matcher("ab89ef");
boolean b = false;
while(b = m.find()) {
System.out.print(m.start() + m.group());
}
}
}

--------------------------------------------------------------------------------



In this code acording to me the index will start from
0-a
1-b
2-8
3-9
4-e
5-f
since we have * it means all are true for matching, but the output is
01289456. How is 6 in the output and how does the regex engine work to produce this output. Kindly explain the same.

Thanks and best Regards,
Pradeep
[ November 07, 2008: Message edited by: Pradeep Kumar ]
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well lets first break this output

0 1 289 4 5 6

when you say \\d*, it means that zero or more occurrences of numerical characters. So for the input ab89ef, it will start as

at 0 zero occurrences of \\d* found

at 1 zero occurrences of \\d* found

at 2 two occurrences of \\d* found so 89 is also displayed

at 4 zero occurrences of \\d* found

at 5 zero occurrences of \\d* found

at 6 zero occurrences of \\d* found

Here you will also have to note that an index or character once used will not be reused. This is why it matches 89 only once. And Pradeep please use code block instead of writing code: manually...
[ November 07, 2008: Message edited by: Ankit Garg ]
 
Pradeep Kumar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But how cum 6 gets printed. The string was "ab89ef". There is no index for 6
index0-a
1-b
2-8
3-9
4-e
5-f
But how 6 gets printed??
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pradeep,
Check out this URL for the answer of your question...

http://faq.javaranch.com/java/ScjpFaq#kb-regexp
 
That's a very big dog. I think I want to go home now and hug 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