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

How to replace multiple matched patterns with multiple patterns?

Last post 11-10-2008, 5:32 PM by Aussie Susan. 2 replies.
Sort Posts: Previous Next
  •  11-10-2008, 2:20 PM 48164

    How to replace multiple matched patterns with multiple patterns?

    Hi,

     Let me explain the scenerio: I have the input text "Mr. XYZ is a very nice person and have a very good taste of humor"

    I want to write one replace string and once pattern string such that I can do the following things:

    er -> gh

    av -> th

    these are two papperts that I can match with the pattern string (?<First>er)|(?<Second>av)

    Suppose I have the replace string as (?<Third>gh)|(?<Fourth>th)

    I want to replace first with the third, and second with fourth.

    Is it possible? if yes how?

    Thank you in advance,

    Abbas Malik

    Filed under: , ,
  •  11-10-2008, 3:43 PM 48169 in reply to 48164

    Re: How to replace multiple matched patterns with multiple patterns?

    Use real data for your sample.  Show the before and after versions of your text. State which language you are doing this in. Basically follow the posting guidelines. http://regexadvice.com/forums/thread/47451.aspx

     


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  11-10-2008, 5:32 PM 48177 in reply to 48164

    Re: How to replace multiple matched patterns with multiple patterns?

    If I understand the general principle behind your question then no, it is not possible to perform different substitutions based on what is matched. It would require the replacement string to contain some processing components (e.g. if match group #1 is not null then use this else use that) but all it can do is insert the match group text.

    The way around this is to perform the substitutions sequentially: ie find all of the first pattern and performs the substitutions, and then find all of the second pattern and  perform those replacements. The only issue you will need to watch out for is where you will generate the second pattern as a result of the first replacement.

    Susan

View as RSS news feed in XML