开发者

.NET Regex Replace Single Line Matching Unknown Character

开发者 https://www.devze.com 2023-04-11 13:41 出处:网络
This has me extremely baffled. Why am I getting duplicate replace strings in the following code: static void Main(string[] args)

This has me extremely baffled. Why am I getting duplicate replace strings 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 outputs to the console:

replacereplace

I understand that regex gets weird matching end line characters but there should be none. I also understand that the pattern can match nothing, but clearly the input is not nothing. This happens in .Net 3.5 and 4.0 and I get the same thing with SingleLine and MultiL开发者_StackOverflow中文版ine.

I know there are several alternatives that will do what I'm expecting but I'm wondering more about what other match .* thinks its finding.


The reason you get two replacements is because with .* you get two matches: "test", and "".

If you change .* to .+ it will work the way you expect it to:

String pattern = ".+";

Another option is to add the start of string anchor:

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


It matches nothing and then it matches everything therefore you have two matches and two replaces.

0

精彩评论

暂无评论...
验证码 换一张
取 消