| Author |
Pattern for phone number which are only digits or having a whitespace/dash after 3 numbers
|
Jacob Sonia
Ranch Hand
Joined: Jun 28, 2009
Posts: 164
|
|
Hi,
Could anyone explain me this pattern for the scenario mentioned in the subject line:
String pattern = "\\d\\d\\d([,\\s])?\\d\\d\\d\\d";
|
 |
Andrew Monkhouse
author and jackaroo
Marshal Commander
Joined: Mar 28, 2003
Posts: 10892
|
|
Have you looked at what each of these mean in the Pattern class?
If so, what part of the regular expression are you having difficulty understanding?
|
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26496
|
|
\\d\\d\\d - 3 digits
[,\\s] - a comma or whitespace character (it should be a dash not a comma)
() - thèse don't buy anything
? - optional (so 7 consecutive digits are ok)
\\d\\d\\d\\d - four digits
Also it would be better to write \\d{3} rather than list them all out.
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
|
 |
Andrew Monkhouse
author and jackaroo
Marshal Commander
Joined: Mar 28, 2003
Posts: 10892
|
|
Jeanne Boyarsky wrote:() - thèse don't buy anything
The only thing that the brackets buy you is that they become a capturing group. That is, if all you care about is finding out what the delimiter (if any) is, then by putting the [,\\s] inside brackets - ([,\\s]) - you will be able to find out what was in the line. Again - the Pattern class has more information.
|
 |
 |
|
|
subject: Pattern for phone number which are only digits or having a whitespace/dash after 3 numbers
|
|
|