tags:

views:

233

answers:

1

How do I susbtitute in matched groups from 1 regular expression into another regular expression in C#?

I need to process an ENUM DNS record where the first half of the record is a regular expression to apply to the lookup value and the second half is a regular expression that uses the matches from the first.

Example of an ENUM record for a lookup on +18001234567

!^\+1800(.*)$!sip:1641641800\[email protected]!

The separate regular expressions are delimited by the ! character and are:

^\+1800(.*)$
sip:1641641800\[email protected]

The correct result of applying the two expressions is:

sip:[email protected]

I can do it be iterating through the matches and using a crude string search and replace but I'm hoping there is a better way. I'm pretty sure in Perl and other languages I could do somehting like:

"+18001234567" =~ s/^\+1800(.*)$/sip:1641641800\[email protected]/
A: 

I ended up doing a crude string substitution replace \n with ${n}.

Regex.Replace(enumSubstitution, @"\\(?<digit>\d)", @"${${digit}}");
sipwiz