| Author |
Regular expression question
|
Payam Fard
Ranch Hand
Joined: Jan 31, 2003
Posts: 65
|
|
Hi all, Does anyone see any problems with the following regular _expression? [(\\(\\d{3}\\)[-]?\\d{3}[-]?\\d{4})?][(\\d{3}[-]?\\d{3}[-]?\\d{4})?] The first [] is supposed to detect (123)456-7890 and the second [] is supposed to detect 123-456-7890 where also 1234567890 is valid. It is also a bit forgiving, if you mistype it as: 123-4567890, it would take it. First [] and second [] work fine separatley, but as soon as I put them together to create an OR, I get a parse exception. Any clues as to why? Thanks Payam.
|
 |
Jason Menard
Sheriff
Joined: Nov 09, 2000
Posts: 6450
|
|
Try this (I made the assumption that the phone number should be the only thing in the string): ^\\(?\\d{3}\\)?-?\\d{3}-?\\d{4}$ This will match for a ten digit sequence with or without parenthesis around the area code, and with or without dashes after the area code and exchange. [ January 31, 2003: Message edited by: Jason Menard ]
|
Jason's Blog
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
See also this thread for some other patterns with slightly different behavior. As for what's wrong with your attempt here... First [] and second [] work fine separatley, but as soon as I put them together to create an OR, I get a parse exception. How exactly do you "put them together to create an OR"? Do you include all the [ and ] characters as shown? (This would be interpreted as a character class, which probably is not what you want here.) Do you just put the two expressions next to each other, as show (which represents a sequence, not an OR) or do you include a | to represent an OR? [ January 31, 2003: Message edited by: Jim Yingst ]
|
"I'm not back." - Bill Harding, Twister
|
 |
Payam Fard
Ranch Hand
Joined: Jan 31, 2003
Posts: 65
|
|
Jason, Thanks for your reply. It works fine. Jim, I was trying to creating an or. I was putting them together as shown. Could you please let correct me on oring the two expressions together? Thanks again, Payam.
|
 |
 |
|
|
subject: Regular expression question
|
|
|