How can I modify this regex so it matches “(” only if “)” is present and vice-versa? This is what I have come up with so far:
1?\(?\d{3}\)?[- ]?\d{3}[- ]?\d{4}
I also tried doing this:
1?\((?=\d{3}\))?[- ]?\d{3}[- ]?\d{4}
(Is there a way to do this one below more efficient? By the way this only matches the parenthesis I’m just testing it. The problem with this one is that I can only match “(” and “)” if they are both present)
\((?=\d{3}\))|(?<=\d{3})\)
Also is there a way to make one regex to complete all the tests, or will I have to make multiple regex?
Hello, being a project for certification, it is best that you try different options, you can make a single regex, which will be quite long, as in my case, and it will be “not very readable”. As a hint I can pass you the beginning of my solution: ^(1)?(\s)?(\d{3}|\(\d{3}\))...
As an alternative to a regex that is too long, I made an array with different regexes for the different types of cases, obviously it is a “cleaner” solution to making a single regex.