.NET Regex Replace Single Line Match Unknown Character

It really puzzled me. Why am I getting duplicate rows in the following code:

static void Main(string[] args)
{
    String input = "test";
    String pattern = ".*";
    String replacement = "replace";
    Console.WriteLine(Regex.Replace(input, pattern, replacement));
    Console.Read();
}

This is displayed on the console:

replacereplace

I understand that the regex gets weird matching end-line characters, but there shouldn't be any. I also understand that a template cannot match anything, but it is clear that the input is nothing. This happens in .Net 3.5 and 4.0, and I get the same with SingleLine and MultiLine.

I know that there are several alternatives that will do what I expect, but I’m more interested in what the other coincidence is. * thinks about his search.

+5
source share
2 answers

, , , .* : "test" "".

.* .+, , :

String pattern = ".+";

- :

String pattern = "^.*"; // I know this looks like a smiley
+4

, , .

+2

All Articles