• Post Reply Bookmark Topic Watch Topic
  • New Topic

Locating data via pattern matching  RSS feed

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23513
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23513
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
thank you James
this is very helpful

the problem is solved and
mission accomplished
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!