• 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

regex questions from KB book

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

import java.util.regex.*;
public class Test4 {
public static void main(String[] args) {
Pattern p=Pattern.compile("\\d*");
Matcher m=p.matcher("ab34ef");
while (m.find()) {
System.out.print(m.start()+m.group()+"\n");
}
System.out.println("------");
Pattern p2=Pattern.compile(".*xx");
Matcher m2=p2.matcher("yyxxxyxx");
while (m2.find()) {
System.out.print(m2.start()+"->"+m2.group()+"\n");
}
System.out.println("------");
String s="ab.cde.fg";
String[] tokens=s.split("\\.");
for(String s2 : tokens) System.out.println(s2);
}
}

Output:
0
1
234
4
5
6(???)
------
0->yyxxxyxx(???)
------
ab
cde
fg

Q1:Why I get value 6 in the last iteration? I think it should loop up to 5.
Q2:Can anybody explain more detail how it works?
Q3:I mixed up all concepts of "\d", "\\d", ".", "\." and "\\." Can anybody explain them how to use in the code?
Thanks a lot.
 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See:
Doubt on Pattern and Matcher!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic