This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • 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.
 
The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic