using regex to replace multiple strings at once ?

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....
Thanks
Carl3003Asked:
Who is Participating?
 
eternal_21Commented:
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."
0
 
YurichCommented:
hmm...
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)

regards
0
 
ptmcompCommented:
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).
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.