Manny
Manny

Reputation: 63

Phone Regex (555) 555-5555 with out using brackets {}

I need to match phone numbers with this format

(555) 555-5555

I have found around this

\(?\b[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}\b

I know that is the correct way but in the platform that I am using there is a bug that removes the {} brackets out of the regex code. I have already reported the bug but it might take a while for them to fix it and release the update so I am wondering if there is another way to accomplish the same with out using brackets.

Upvotes: 0

Views: 1815

Answers (4)

AaronM
AaronM

Reputation: 2459

Simply replace the brackets with the given number of iterations of the sequence before the bracket.

\(?\b[0-9][0-9][0-9]\)?[-. ]?[0-9][0-9][0-9][-. ]?[0-9][0-9][0-9][0-9]\b

Upvotes: 0

Kirk Strauser
Kirk Strauser

Reputation: 30947

Well, sure. Instead of saying "apply pattern {n} times", write "patternpatternpattern..." n times:

\(?\b[0-9][0-9]0-9]\)?[-. ]?[0-9][0-9][0-9][-. ]?[0-9][0-9][0-9][0-9]\b

Upvotes: 0

epascarello
epascarello

Reputation: 207511

just use \d for each characcter you need to match

[0-9]{3} is \d\d\d

[0-9]{4} is \d\d\d\d

Upvotes: 0

Smern
Smern

Reputation: 19076

[0-9]{3} is the same thing as [0-9][0-9][0-9] or \d\d\d

In full that would look like this:

\(?\b[0-9][0-9][0-9]\)?[-. ]?[0-9][0-9][0-9][-. ]?[0-9][0-9][0-9][0-9]\b

or

\(?\b\d\d\d\)?[-. ]?\d\d\d[-. ]?\d\d\d\d\b

Upvotes: 5

Related Questions