Regex - conditional replacement if the captured group exists

Suppose I have the following two lines containing phone numbers:

  • 1112223333
  • 11122233334

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:

  • 1112223333 > (111) 222-3333 ext (should be (111) 222-3333 (without the suffix "ext")

  • 11122233334 > (111) 222-3333 ext 4 (correct)

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?

+7
regex
source share
1 answer

Well, the closest I could get to this is using a re-evaluation of the conformity assessment with interpolation of C # 6 strings.

Example using C # 6 string interpolation:

 var phone = "01234567894"; var txt = Regex.Replace( phone, @"^(\d{3})(\d{3})(\d{4})(\d?)$", m => $"({m.Groups[1]}) {m.Groups[2]}-{m.Groups[3]}{(m.Groups[4].Success ? " ext " + m.Groups[4].Value : "")}"); 

Or if using old C # using String.Format :

 var phone = "01234567894"; var txt = Regex.Replace( phone, @"^(\d{3})(\d{3})(\d{4})(\d?)$", m => String.Format("({0}) {1}-{2}{3}", m.Groups[1], m.Groups[2], m.Groups[3], m.Groups[4].Success ? " ext " + m.Groups[4].Value : "")); 
+1
source share

All Articles