This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regular expression question

 
Payam Fard
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Payam Fard
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic