using regex to replace multiple strings at once ?

Posted on 2005-04-10
Last Modified: 2011-10-03
To replace multiple strings , i use the expression Regex("asdf sdfd sdfd",str1, someString) mulptiple times like
Regex("asdf sdfd sdfd",str2, someString)
Regex("asdf sdfd sdfd",str3, someString)
Regex("asdf sdfd sdfd",str4, someString)
I want to find a way to combine all these statements into just this one
Regex("asdf sdfd sdfd", any of (str1, str2, str3, str4) , someString)

in other words to replace any occurrence of str1, str2, str3....
Question by:Carl3003
    LVL 21

    Assisted Solution

    one of the ways to do it is to put all your strings in the array when your manipulate them, like (they don't have to be of the same name pattern or anything)

    // depending on locations you might want to declare your variables in the class-scope or
    // just locally
    ArrayList aList = new ArrayList();

    string name = GetName();
    aList.Add( name );

    string somethingElse = GetReference();
    aList.Add( somethineElse );


    string whatEverYouWant = GetWhatEverYouWant();
    aList.Add( whatEverYouWant );

    foreach( string s in ArrayList )
       Regex("asdf sdfd sdfd", s, someString)

    LVL 10

    Accepted Solution

    To replace multiple strings at once, just place the values you are looking for in groups (using '(' and ')'), seperated by an OR ('|') operator.

    For example,

      System.Text.RegularExpressions.Regex.Replace("Thisstr1isstr2astr3test.", "(str1)|(str2)|(str3)", " ");

    will return the string, "This is a test."
    LVL 10

    Expert Comment

    You can call Replace with a MatchEvaluator delegate. The delegate will be called on every match and you can decide how you want to replace the matched string (or if you want to store it somewhere else).

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now