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

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#正则表达式库中的错误吗?为什么它会打印“George”两次,当Perl和Python只打印一次?

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

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

全局替换Perl

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

$a="This is a test";
$a=~s/(.*)/George/g;
print $a;

哪个产生

GeorgeGeorge

在.NET中单个替换

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

哪个产生

George

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

相关文章

jquery.validate使用攻略(表单校验) 目录 jquery.validate...
/\s+/g和/\s/g的区别 正则表达式/\s+/g...
自整理几个jquery.Validate验证正则: 1. 只能输入数字和字母...
this.optional(element)的用法 this.optional(element)是jqu...
jQuery.validate 表单动态验证 实际上jQuery.validate提供了...
自定义验证之这能输入数字(包括小数 负数 ) <script ...