RegEx $0 substitution cannot manipulated (e.g. converted to upper-case)

31 Views Asked by At

Trying to understand why I cannot manipulate the $0 substitution value when working with RegEx in .Net Core. Here is my full code:

    class Program
    {
        static void Main(string[] args)
        {
            var input = "part1-part2-part3";
            var pattern = "-p";
            var regex = new Regex(pattern);
            var result1 = regex.Replace(input, "$0".ToUpper());
            Console.WriteLine(result1);

            //Output is this:
            //part1-part2-part3
            //Would expect this:
            //part1-Part2-Part3
        }
    }

So why doesn't "$0".ToUpper() work here? And what would be way to achieve this in RegEx (capitalize all first letters after each hyphen).

0

There are 0 best solutions below