RegEx.Replace replace more than one

  • Thread starter Thread starter kids_pro
  • Start date Start date
K

kids_pro

Hi there,

Is it possible to use RegEx.Replace to replace string like this.
string str = "The world heavy weight match";

I want to replace world = w and match = m
I can do that with 2 line of RegEx.Replace. I wonder is it possible to do it
with 1 RegEx.Replace?

The example above is simple my real situation is more word to replace I
don't want to store list of word to replace in the Hashtable and loop
through the hash to replace the string it kind of slow (is what I am doing
right now).

Please advice.
Regards,
kids
 
Try RegEx.Replace(str, "(w)orld|(m)atch", "$1$2").
However I'm not sure how fast an enumeration as big as you describe it will
be.

Niki
 
Thanks this is a good trick.
But in my real application is not simple like that.

str = "Is there any faster method in the world to match and replace"
RegEx.Replace(str, "(w)orld|(m)atch", "$1$2").

will return
`w` in the place of `world`
and
`m` in the place of `match`.

But what I want to do is the replace w and m with something else.
Take the same example above. I want to replace world with x and match with
y.
If it possible to do it in a single line of RegEx?

Regards,
Kids
 
Back
Top