Win a copy of TDD for a Shopping Website LiveProject this week in the Testing 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Regex doubt

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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());
}
}
}
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
It runs on an internal combustion engine. This ad does not:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic