using regex to replace multiple strings at once ?

Posted on 2005-04-10
Medium Priority
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 one..like 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

Yurich earned 100 total points
ID: 13750184
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

eternal_21 earned 100 total points
ID: 13750260
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

ID: 13767048
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses

850 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