wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regex doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regex doubt" Watch "Regex doubt" New topic
Author

Regex doubt

sravanthi pulukuri
Ranch Hand

Joined: Mar 15, 2007
Posts: 125
import java.util.regex.*;
public class searchesUsingQuantifiers51{

public static void main(String args[]){
Pattern p = Pattern.compile("\\d\\d\\d([-\\s])?\\d\\d\\d\\d");
Matcher m = p.matcher("123%456 1234567 123 4567 1234-4567");
boolean b = false;
while(b=m.find()){
System.out.println(" At Position " + m.start() + " the o/p is " + m.group());
}
}
}
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Sravanthi,

I will prefer to meaning of pattern rather than output
Meaning of ("\\d\\d\\d([-\\s])?\\d\\d\\d\\d"),

find me:
1.\\d\\d\\d : three consecutive digits, after that
2.([-\\s])? : ? means 0 or 1 occurance of - or \\s(space) , after that
3.\\d\\d\\d\\d: four consecutive digits.

Important Point: Matcher never considers consumed input.
Suppose Things are pretty clear now.


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
 
GeeCON Prague 2014
 
subject: Regex doubt