• 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 about Regular Expression

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I couldnt understand how the above code in the study guide works

import java.util.regex.*;
class Regex2 {
public static void main(String[] args) {
Pattern p = Pattern.compile(args[o]);
Matcher m = p.matcher(args[1]);
boolean b = false;
while(b = m.find()) {
System.out.print(m.start() + m.group());
}
}
}
java Regex2 "\d*" ab34ef
what is the result??
The answer is 01234456

i thought if the expression is "\d*" ,then the first element must be a digit
but here the result says otherwise..if somebody could help me on this it would really be great...
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can I suggest that you search this forum for "Regex2"? This problem has been discussed many times before.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"\d*" matches zero or more digits. so "" also matches for "\d*".
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

instead of just posting the original question by copy and paste you should track down the problem to something simpler.
Simplify the original code!
Don't use the String args as input, simplify the String to be searched and type a line like:
String s="1"; //or "1A";
You can come back to the original "ab34ef" later.

Make the output clearer!
Do not use the cryptic output line of the original question, better output the start() and group() seperated (and perhaps labeled) and not glued together.

Yours,
Bu.
 
vijay krishnamoorthy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay i will do it next time.....thank you very much for clearing my doubt.....
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how come the answer is 01234456... i thought it was 0123445

string: ab34ef

there are 6 characters and it started at index 0. am i missing something. is there an end of string character?

thanks
clark
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question comes up so often that it is actually in the JavaRanch FAQ.

Henry
[ September 11, 2006: Message edited by: Henry Wong ]
 
Clark Reynes
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry

I'll read the rest of the FAQ.
 
reply
    Bookmark Topic Watch Topic
  • New Topic