This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
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

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regex doubt" Watch "Regex doubt" New topic

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;
System.out.println(" At Position " + m.start() + " the o/p is " +;
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%)
subject: Regex doubt