0

I am not understanding why this regex pattern is not matching as expected. I am testing this pattern on https://regexr.com.

regex =/^1?\((?=\d{3}\))[-. ]?(\d{3})$/
sample = 1(123) 123

My understanding is first pattern should be number 1 or nothing then 3 digits in closed parentheses or no parentheses at all. There should not be open parentheses which will be the case when we only use (?(\d{3})\)? and after [-. ] which is optional, followed by 3 digits end.

isherwood
  • 58,414
  • 16
  • 114
  • 157
paldong
  • 3
  • 1

3 Answers3

1

"?=" is ruining your regex since you want to catch those 3 digits. If you want to make paranthesis optinoal but you dont want any open one, you should create to cases then.

check that one if u solve your issue:

regex=/^1?(\((\d{3}\))||(\d{3}))[-. ]?(\d{3})$/
uyanikm
  • 21
  • 3
0

Lookahead doesn't consume symbols. As a result, regex like this \((?=\d{3}\))[-. ] require, that on one hand, ( is followed by three digits and ), but on the other hand followed by either of dash, dot or space.

In your case, you don't need lookaheads at all, simple ^1?\(\d{3}\)[-. ]?(\d{3})$ should work.

Demo here.

markalex
  • 8,623
  • 2
  • 7
  • 32
  • thanks for clearifying my doubt, may I request for doc or any resource to up my regex knowledge. – paldong Jul 02 '23 at 11:07
  • @paldong, you can start with [this short beginners guide](https://stackoverflow.com/questions/4736/learning-regular-expressions). Also, if you have some specific questions, there is a [reference on regexes](https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean). Plus, if you have some doubt's or problems - use regex tool, like regex101.com, to "debug" your expressions; those are usually pretty verbal and help with explaining what regex means, and what is actually matches. – markalex Jul 02 '23 at 12:10
0

I am not 100% sure I read your requirements correctly, but I think you want these scenarios to work?

1(234) 567
1(234)567
(234) 567
(234)567
1234 567
1234567
234 567
234567

And, probably more trickily, you want the 3 matching groups to be the first digit if any, the 3 numbers inside the optional parenthesis, and then the final 3 numbers.

This is my first attempt, using a look-ahead to check for the valid cases of with/without parenthesis, then following that with grabbing the two groups for the two sets of numbers.

^(1?)(?=\d{3}[-. \d]+|\(\d{3}\))\(?(\d{3})\)?[-. ]?(\d{3})$
Tony B
  • 159
  • 6
  • Yes, I was using the lookahead incorrectly, Here with your regex, I have one doubt, since you are using 3digits closure group thrice one each for parentheses options, one with optional parentheses and one without parentheses option at all. So in that case the exact digits in the pattern should be 9 with one extra optional digit at beginning. How its giving true output for only 6 digits. I am not able to understand it and it would be great if there is syntax to set the length for overall pattern and not just for closure group. – paldong Jun 29 '23 at 16:24
  • *capture group () not closure group x – paldong Jun 29 '23 at 17:05
  • It is hard to communicate regex requirements in a forum :) but if I get your question, the look-ahead is simply an assertion to say "yes, this is ahead of me", it does not contribute to the capture groups. It is validating that there are either 3 digits followed by either a gap character or 4th digit, or 3 digits in parenthesis. If that assertion passes, then it just reads the two groups of 3 digits. Hope that helps. – Tony B Jun 30 '23 at 02:52