• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

regex question from K & B

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.regex.*;
class Regex {
public static void main(String [] args) {
Pattern p = Pattern.compile(args[0]);
Matcher m = p.matcher(args[1]);
boolean b = false;
System.out.println("Pattern is " + m.pattern());
while(b = m.find()) {
System.out.println(m.start() + " " + m.group());
}
}
}
********************************************
% java Regex "\d\w" "ab4 56_7ab"
Produces the output
Pattern is \d\w
4 56
7 7a
*********************************************
% java Regex "\w\d" "ab4 56_7ab"
Produces the output
Pattern is \w\d
1 b4
4 56
6 _7

In the above the 2 outputs are quite different. Can somebody explain the outputs wrt the combination if \d\w and \w\d..i understand the difference between \d and \w but dont get it when used in combination. Thanks!
[ January 17, 2008: Message edited by: sridhar row ]
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/d=> digit
/w=> letter,digit or underscore(_)

/d/w searchs for 1st char that is digit and next is either letter,digit or underscore(_). So you got the collowing-:

% java Regex "\d\w" "ab4 56_7ab"
Produces the output
Pattern is \d\w
4 56
7 7a



/w/d searches for 1st char as letter,digit or underscore(_) and the next one should be digit. Here you got the following-:

% java Regex "\w\d" "ab4 56_7ab"
Produces the output
Pattern is \w\d
1 b4
4 56
6 _7



hope this clears
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i guess i do..thanks
[ January 17, 2008: Message edited by: sridhar row ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic