在Python中:
ttsiod@elrond:~$ python
>>> import re
>>> a='This is a test'
>>> re.sub(r'(.*)', 'George', a)
'George'
在Perl中:
ttsiod@elrond:~$ perl
$a="This is a test";
$a=~s/(.*)/George/;
print $a;
(Ctrl-D)
George
在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);
}
}
}
不幸的是,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
这是C#正则表达式库的一个bug吗?为什么会打印"George"两次,而Perl和Python只会打印一次呢?