0

Regex:

\b(\(\d{3}\)|\d{3})?[-.]?\d{3}[-]?\d{4}\b

My input file has two types of phone numbers. One, whose first 3 digits are enclosed in parenthesis and the other with no parenthesis. Eg:

"(201)-450-4479" ,"234-345-3456"

I want to match both type of phone numbers using alternate operator.

Please suggest me. What modification is required for above mentioned expression to get the intended result?

serenesat
  • 4,611
  • 10
  • 37
  • 53
Pramod
  • 1

2 Answers2

1

\b matches at a word-nonword boundary. If such a boundary should appear before (, it must be preceded by a word character, not whitespace or nothing.

Cf.

print /\b\(/ ? 1 : 0 for '(', ' (', 'a(';

Remove the starting \b from the regex, or replace it with

(?x: \b | \s | ^ )
choroba
  • 231,213
  • 25
  • 204
  • 289
0

I'd use this:

(\(?\d+\)?\-\d+\-\d+)

or using the alternate operator:

(\d+\-\d+\-\d+|\(\d+\)\-\d+\-\d+)

fugu
  • 6,417
  • 5
  • 40
  • 75
  • Thanks for the answer. Both of your above expressions are yielding expected results. I am just curious to understand what was wrong with my expression? – Pramod May 18 '15 at 13:54
  • Could you please suggest me what's wrong with this expression "((\d+|\(\d+\))\-\d+\-\d+)" . Result of this exp is "(201)-340-2011 (201) 234-345-3456 234 (201)-450-4479 (201)". Duplicates of first three digits are printing after every successful match. – Pramod May 18 '15 at 14:36
  • @Pramod - first you need to escape the `(` characters like this: `\`(` – fugu May 18 '15 at 14:40
  • @Pramod but that regex, when escaped properly, will capture all digits if parentheses are not present, or the first 3 digits only if they are. I don't think that's what you want – fugu May 18 '15 at 14:41
  • Indeed i have escaped the parentheses using backslash but i don't know why those escape characters are not visible in the comment. I am just trying to avoid the redundant characters in the expression, – Pramod May 18 '15 at 15:20