0

If I have lots of phone numbers like

123-456-7890
(111-111-1111
111)-111-1111
(123)-456-7890

I just want to match no.1 and no.4, my pattern can only match the no.4, and It seems that the if condition doesn't work.

(\()(\d+)(?(1)\)\-|\-)(\d+\-\d+)

All the solutions in regex websites can't recognize single bracket.

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
yanwii
  • 170
  • 2
  • 12
  • You can find in Regex sites like [RegExr](https://regexr.com/), check the "Community Patterns" section. – trinaldi Apr 09 '19 at 06:20
  • As I said, those solutions can not filter the single bracket, it's a different problem. @Michał Turczyn – yanwii Apr 09 '19 at 07:47
  • Please reopen this post as the post isn't only about validating a US number but OP also has a problem as to why his `If Clause` in the regex didn't work which needs a separate explanation and solution. – Pushpesh Kumar Rajwanshi Apr 09 '19 at 09:04

2 Answers2

1

You can use this regex, which matches either case, with parenthesis OR without parenthesis using alternations,

^(?:\(\d+\)|\d+)-\d+-\d+$

Also use start/end ^/$ anchors to ensure the regex doesn't allow any partial matches.

Regex Demo 1

In case you want to match the number of digits exactly like in sample, you can make the quantifiers more specific and use this regex,

^(?:\(\d{3}\)|\d{3})-\d{3}-\d{4}$

Regex Demo 2

Edit: Explanation and correction of OP's regex which uses If Clause in regex

In your regex, you need to turn group1 as optional by putting a ? after group1

(\()(\d+)(?(1)\)\-|\-)(\d+\-\d+)
^^^^ This is mandatory which stops it to match a number that doesn't have ( in start

Hence the correct version of your regex should be,

^(\()?(\d+)(?(1)\)\-|\-)(\d+\-\d+)$
     ^ You need to add this to make group1 optional so it can match a number without `(`

Also, as you can see, I've used ^ and $ so the regex doesn't allow partial match in the number.

Check this demo with your own updated regex, which works like you expected

Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36
  • Nice! And do you know why the if condition doesn't work? have participated in a interview and the interviewer told me use reference. – yanwii Apr 09 '19 at 06:35
  • The term you are looking for is "back reference". It doesn't directly help here, though, as far as I can see. – tripleee Apr 09 '19 at 07:21
  • @yanwii: Updated my answer with explanation of why your regex with `If Clause` didn't work as expected and how to make it work. Although like I posted my answer, you can make it work even without `If Clause`. – Pushpesh Kumar Rajwanshi Apr 09 '19 at 09:02
  • 1
    Thank you very much! It seems that there are some people don't know how to understand the question. – yanwii Apr 09 '19 at 09:13
0

Try ^(?(?=\()\(\d{3}\)|\d{3})-\d{3}-\d{4}$

Demo

Explanation:

^ - beginning of the string

(?(?=\()\(\d{3}\)|\d{3}) - conditional expression: if positiva lookahead (?=\() is successfull (what follows is bracket (), then match \(\d{3}\) - three digits between brackets, otherwise, match \d{3} - three digits

-\d{3}-\d{4} - match hyphen, three digits, hyphen and four digits

$ - end of string

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69