This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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: 23590
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: 23590
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
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!