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