Под Питоном:
ttsiod@elrond:~$ python
>>> import re
>>> a='This is a test'
>>> re.sub(r'(.*)', 'George', a)
'George'
Под Перлом:
ttsiod@elrond:~$ perl
$a="This is a test";
$a=~s/(.*)/George/;
print $a;
(Ctrl-D)
George
Под С#:
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);
}
}
}
К сожалению, С# печатает:
$ 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#? Почему он печатает «Джордж» два раза, тогда как Perl и Python печатают его только один раз?
$a=~s/(.)/X/;
даетXhis is a test
под Perl. 31.08.2011