Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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 ]
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic