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?

1
  • 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. Commented Dec 28, 2017 at 8:23

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

2 Comments

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 . 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 ? Best Regards
@KorkOoO: I moved all the logic into the lambda: m => ..., while regular expression itself ([0-2]) hardly worth testing…
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

Comments

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#

Comments

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;
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.