• 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:

Locating data via pattern matching

 
Greenhorn
Posts: 20
  • 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


how can i know how \d\w pattern works

eg if we looking for a digit (\d)we will gey the 4 which has index 2
and the word wg we may get the whole 56_7ab which start at index 4

is digit && word
or digit || word

i really dont understant how those metacharcters works together as in the exmaple \d\w



 
author
Posts: 23959
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
Well. the Java tutorial is a place to start....

http://java.sun.com/docs/books/tutorial/essential/regex/

Although, admittingly, there are better tutorials out there. As regex is pretty standard with many different scripting languages.

Henry
 
Henry Wong
author
Posts: 23959
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

% java Regex "\d\w" "ab4 56_7ab"



Basically, a "\d" means a digit -- a single character that can be between a "0" or a "9". A "\w" is a word character -- a single character that can be a digit, or a letter, or a underscore. When put together, it means a digit followed by a word character... so it should match these two parts of your input... "ab4 56_7ab"

Henry
 
Hossam El-Gazzaz
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think that helped me much but i hava a another question

why we got the 7a only
not the 7ab
isnt 7ab make a word too
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hossam El-Gazzaz wrote:
why we got the 7a only
not the 7ab
isnt 7ab make a word too



"W" can only match a single character.
If we want that to match more,i.e zero ,one or more than one ,you have to use characters like * ,?
 
Hossam El-Gazzaz
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you James
this is very helpful

the problem is solved and
mission accomplished
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic