• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex doubt

 
sravanthi pulukuri
Ranch Hand
Posts: 125
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 557
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic