Code
using System;
using System.Text.RegularExpressions;
namespace RegexNoMatch {
class Program {
static void Main () {
string input = "a foobar& b";
string regex1 = "(foobar|foo)&?";
string regex2 = "(foo|foobar)&?";
string replace = "$1";
Console.WriteLine(Regex.Replace(input, regex1, replace));
Console.WriteLine(Regex.Replace(input, regex2, replace));
Console.ReadKey();
}
}
}
Expected output
a foobar b
a foobar b
Actual output
a foobar b
a foobar& b
Question
Why does replacing not work when the order of "foo" and "foobar" in regex pattern is changed? How to fix this?