2

I'm trying to perform multiple replacements using regular expressions in a string. Currently, I'm using a regex command (mentioned below) in Notepad++ , and it works as expected; however, when I tried to test the same regex on regexstorm it didn't work.

Assume that I want to find & replace the following:

  • 0 > A
  • 1 > B
  • 2 > C

Input:

5441.32 6140 
1:34,360

Find: (0)|(1)|(2)

Replace: (?1A)(?2B)(?3C)

NP++ Output:

544B.3C 6B4A
B:34,36A

regexstorm Output:

544(?1A)(?2B)(?3C).3(?1A)(?2B)(?3C) 6(?1A)(?2B)(?3C)4(?1A)(?2B)(?3C) 
(?1A)(?2B)(?3C):34,36(?1A)(?2B)(?3C)

I'm still reading and learning about regular expressions so, could somebody please tell me what am I doing wrong on regexstorm?

KorkOoO
  • 522
  • 1
  • 4
  • 15
  • 1
    Well, different regex engines have different feature sets. Notepad++ is using Boost, where conditional replacements are possible, which isn't the case for most other regex engines. – Sebastian Proske Dec 28 '17 at 08:23

4 Answers4

2

Why not Linq which is simpler than regular expression:

string source = "5441.32 6140";

string result = string.Concat(source
  .Select(c => c >= '0' && c <= '2' 
     ? (char) (c - '0' + 'A')
     : c));

If you want to update the condition and add, say 3 > D, all you have to do is to change c <= 2 into c <= 3. If you insist on regular expression, I suggest working with match, not with pattern:

string source = "5441.32 6140";

string result = Regex.Replace(source, 
  "[0-2]",                                            // easy pattern
   m => ((char)(m.Value[0] - '0' + 'A')).ToString()); // elaborated match
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • Thanks a lot for the the quick respond bro :) plz excuse my lake of knowledge, but I still don't know how to use your code on [regexstorm](http://regexstorm.net/tester?p=%280%29%7c%281%29%7c%282%29&i=5441.32+6140+%0d%0a1%3a34%2c360&r=%28%3f1A%29%28%3f2B%29%28%3f3C%29) . My problem is, I'm using a software that suggested regexstorm in the first place to build/test my own regex before using them so, I wonder is it even possible to perform such multiple replacements using the regex engine of [regexstorm](http://regexstorm.net/tester) ? Best Regards – KorkOoO Dec 28 '17 at 09:51
  • @KorkOoO: I moved all the logic into the lambda: `m => ...`, while regular expression itself (`[0-2]`) hardly worth testing… – Dmitry Bychenko Dec 28 '17 at 11:59
1

You can simply do string replace-

string input = "5441.32 6140";
string output = input.Replace('0', 'A').Replace('1', 'B').Replace('2', 'C');
Console.WriteLine(output); //output is 544B.3C 6B4A
Souvik Ghosh
  • 4,456
  • 13
  • 56
  • 78
1

You can do this as follow,

 static void Main(string[] args)
    {
        string pattern = @"(0|1|2)";
        string input = "5441.32 6140";
        Regex rgx = new Regex(pattern);
        var map = new Dictionary<string, string> {
                  { "0", "A"},
                  { "1", "B"},
                  { "2", "C" }
                 };

        string result = rgx.Replace(input,m => map[m.Value]);

        Console.WriteLine("Original String:    '{0}'", input);
        Console.WriteLine("Replacement String: '{0}'", result);
        Console.ReadLine();
    }

Its better to use StringBuilder. Link:Replace Multiple String Elements in C#

OOMMEN
  • 249
  • 3
  • 6
0
var s = "5441.32 6140";
var pattern = @"(0|1|2)";
var s2 = Regex.Replace(s, pattern, m =>
{
    string sMatch = m.Value;
    string final = null;
    if (sMatch == "0")
        final = "A";
    else if (sMatch == "1")
        final = "B";
    else if (sMatch == "2")
        final = "C";
    return final;
});
JohnyL
  • 6,894
  • 3
  • 22
  • 41