加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-13 22:54:06 所属栏目:百科 来源:网络整理
导读:在 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.Collectio
在 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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读