コード
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();
}
}
}
期待される出力
a foobar b
a foobar b
実際の出力
a foobar b
a foobar& b
質問
正規表現パターンの「foo」と「foobar」の順序を変更すると、置換が機能しないのはなぜですか? これを修正する方法は?