Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6473973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:31:48+00:00 2026-05-25T06:31:48+00:00

Under Python: ttsiod@elrond:~$ python >>> import re >>> a=’This is a test’ >>> re.sub(r'(.*)’,

  • 0

Under Python:

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

Under Perl:

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

George

Under 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);
    }
  }
}

Unfortunately, C# prints:

$ 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

Is this a bug in the regular expression library of C# ? Why does it print “George” two times, when Perl and Python just print it once?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:31:49+00:00Added an answer on May 25, 2026 at 6:31 am

    In your example the difference seems to be in the semantics of the ‘replace’ function rather than in the regular expression processing itself.

    .net is doing a “global” replace, i.e. it is replacing all matches rather than just the first match.

    Global Replace in Perl

    (notice the small ‘g’ at the end of the =~s line)

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

    which produces

    GeorgeGeorge
    

    Single Replace in .NET

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

    which produces

    George
    

    since it stops after the first replacement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Under Python 3.1, when trying to run this code: from urllib import request def
I am currently solving a system of differential equation under python using odeint to
I'm using Windows 7, and I've tried this under Python 2.6.6 and Python 3.2.
I'm using the pywin32 extensions to access the win32 API under Python. I'm new
This code works on Debian under Python 2.5 but doesn't on Ubuntu under Python
I was trying to understand FIFOs using Python under linux and I found a
I tested these under python 2.6 and 2.7. See this is OK: >>> exec
Has anyone seen anything in Tix work under python 3.0? I've tried to work
I can't seem to get the wsgiref module to work at all under Python
I'm trying to run some python code under Apache 2.2 / mod_python 3.2.8. Eventually

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.