Need help in regex replace method.
-
Hi all, Can anybody provide me the regex to replace the '&' with '&' but the '&' character in word like '&','<' '>' etc shoud be ignored. Frankly speaking I am weak in regular expression. I would be highly thankful if somebody provide me the good learning articles on this. Thanks Rohit
-
Hi all, Can anybody provide me the regex to replace the '&' with '&' but the '&' character in word like '&','<' '>' etc shoud be ignored. Frankly speaking I am weak in regular expression. I would be highly thankful if somebody provide me the good learning articles on this. Thanks Rohit
It's not too complex, if you do it via a MatchEvaluator, as you will probably have to examine each case of '&' in context:
Regex regexWithAllPossible = new Regex(@"(>|<|&|&)");
string result = regexWithAllPossible.Replace(source, new MatchEvaluator(CheckMatch));
...
private string CheckMatch(Match m)
{
if (m.Value == "&")
{
return "&";
}
return m.Value;
}Remember to list all the possibilities in the regex! Get a copy of Expresso[^] - it decodes, creates, and tests regexs! It's free and really helps.
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
-
It's not too complex, if you do it via a MatchEvaluator, as you will probably have to examine each case of '&' in context:
Regex regexWithAllPossible = new Regex(@"(>|<|&|&)");
string result = regexWithAllPossible.Replace(source, new MatchEvaluator(CheckMatch));
...
private string CheckMatch(Match m)
{
if (m.Value == "&")
{
return "&";
}
return m.Value;
}Remember to list all the possibilities in the regex! Get a copy of Expresso[^] - it decodes, creates, and tests regexs! It's free and really helps.
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
-
You are welcome! Expresso is a good piece of kit - I've been using it for about a year and really wish I'd written it.
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy