1

In this input:

hello Sam, how are you 27‏ do not worry 5
how are you  why you are not OK.ℏ  and ‍ 

I would like to perform some conditional replacements:

"2" => "a"
"7" => "b"
"5" => "c"

I also need to exclude strings such as ‏ and  , for which we can use this pattern: &#x\w+;

Here is what I started with. However, I don't know how to exclude the pattern.

inputstring="hello Sam, how are you 27‏ do not worry 5
how are you  why you are not OK.ℏ  and ‍";
string reg = @"2!(&#x\w+;)";
Regex myRegex = new Regex(reg);
string newstring = myRegex.Replace(inputstring, "a");
zx81
  • 41,100
  • 9
  • 89
  • 105
Khosro
  • 707
  • 1
  • 7
  • 16
  • 2
    Why don't you start coding and then show us where do you have problems? Or do you want just us to write code for you? – Steve Jul 20 '14 at 08:58
  • No, i do not want to write code for me.I do not exactly know how to exclude a pattern and then replace.I updated my question with my code.But i know this is poor code. – Khosro Jul 20 '14 at 09:11
  • But now is a question with something to look for. – Steve Jul 20 '14 at 09:13

1 Answers1

3

Delegate Replacement Function

We can use this surprisingly simple regex:

&#x\w+;|([257])

This problem is a classic case of the technique explained in this question to "regex-match a pattern, excluding..."—and a particularly interesting one because of the different replacements depending on the digits matched.

In the regex, the left side of the alternation | matches complete html entities. We will ignore these matches (we match them to neutralize them). The right side matches the digits 2, 5 or 7, and we know they are the right ones because they were not matched by the expression on the left.

This program shows how to use the regex (see the results at the bottom of the online demo):

    var myRegex = new Regex(@"&#x\w+;|([257])");
    string s1 = @"hello Sam, how are you 27‏ do not worry 5
how are you  why you are not OK.ℏ  and ‍ ";

    string replaced = myRegex.Replace(s1, delegate(Match m) {
    switch (m.Groups[1].Value) {
        case "2": return "a";
        case "7": return "b";
        case "5": return "c";
        default: return  m.Value;
        }
    });
    Console.WriteLine(replaced);

Output:

hello Sam, how are you ab‏ do not worry c
how are you  why you are not OK.ℏ  and ‍ 

Reference

Community
  • 1
  • 1
zx81
  • 41,100
  • 9
  • 89
  • 105
  • Thanks Khosro—yes, it's a terrific technique, and your question, with the different replacements for the various numbers, was a wonderful example. :) – zx81 Jul 20 '14 at 11:04