Got more questions? Find advice on: ASP | SQL | XML | Windows
Welcome to RegexAdvice Sign in | Join | Help

Replacing elements of a pattern as part of the matching process?

  •  09-18-2009, 11:48 PM

    Replacing elements of a pattern as part of the matching process?

    I am trying to matching a sub group, but replace the content of that sub group in the resulting string with something else.  For example, say I want to match a phone number like 800-123-4567 with \d3-\d{3}-\d{4} but get out of it 800-1234 567 (replacing the '-' with ' ' but only within the number, not after the area code). I know I can call Regex to do a replace based on the sub group index within the regex, but this has significant drawbacks: it requires making another call and the sub group index could change any time the pattern changes and break the code. I would much rather have the substitution be specified right in the regex next to the definition of the sub group.

     Thanks!

    Filed under:
View Complete Thread