Suppose I have the following two lines containing phone numbers:
The first is for a regular phone number (111) 222-3333 , and the second is for a phone number with the extension (111) 222-3333 ext 4
So, we know that the phone number will always be 10 digits and possibly 11. If it is 11, then I would like it to be formatted with the second version.
My current regular expression and its replacement:
Regex: (\d{3})(\d{3})(\d{4})(\d?)
Replacement: ($1) $2-$3 ext $4
Which works, except that regardless of whether the fourth capture group exists or not, I get the added "ext", so I get:
I know I can do this with code / evaluation matches (I program in C # /. Net), but I'm more curious to find out if there is a way to change the replacement regular expression to have some form of logic, just add the suffix ext $4 then and only when there was a 4th capture group?
regex
John bustos
source share