正则表达式 - C#的行为与Perl/Python不同

tts*_*ras 6 c# regex

在Python下:

ttsiod@elrond:~$ python
>>> import re
>>> a='This is a test'
>>> re.sub(r'(.*)', 'George', a)
'George'
Run Code Online (Sandbox Code Playgroud)

在Perl下:

ttsiod@elrond:~$ perl
$a="This is a test";
$a=~s/(.*)/George/;
print $a;
(Ctrl-D)

George
Run Code Online (Sandbox Code Playgroud)

在C#下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Text.RegularExpressions;

namespace IsThisACsharpBug
{
  class Program
  {
    static void Main(string[] args)
    {
        var matchPattern = "(.*)";
        var replacePattern = "George";
        var newValue = Regex.Replace("This is nice", matchPattern, replacePattern);
        Console.WriteLine(newValue);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,C#打印:

$ csc regexp.cs
Microsoft (R) Visual C# 2008 Compiler version 3.5.30729.5420
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.

$ ./regexp.exe 
GeorgeGeorge
Run Code Online (Sandbox Code Playgroud)

这是C#正则表达式库中的错误吗?为什么它会打印"George"两次,当Perl和Python只打印一次?

Gry*_*ynn 6

在您的示例中,差异似乎在"替换"函数的语义中,而不是在正则表达式处理本身中.

.net正在进行"全局"替换,即它正在替换所有匹配,而不仅仅是第一次匹配.

全局替换Perl

(注意= ~s行末尾的小'g')

$a="This is a test";
$a=~s/(.*)/George/g;
print $a;
Run Code Online (Sandbox Code Playgroud)

哪个产生

GeorgeGeorge
Run Code Online (Sandbox Code Playgroud)

在.NET中单个替换

var re = new Regex("(.*)");
var replacePattern = "George";
var newValue = re.Replace("This is nice", replacePattern, 1) ;
Console.WriteLine(newValue);
Run Code Online (Sandbox Code Playgroud)

哪个产生

George
Run Code Online (Sandbox Code Playgroud)

因为它在第一次更换后停止.