Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Regex Replace and *

Tags:

c#

perl

I'm a perl programmer doing a bit of C#. Facing an odd issue with Regex.Replace in regard to the zero-or-more assertion, *.

Say I wanted to replace zero or more letters with a single letter. In perl, I could do this:

my $s = "A";
$s =~ s/\w*/B/;
print $s;
$s now = "B"

But if I try and do the same in C#, like this:

string s = Regex.Replace("A", @"\w*", "B");
s now = "BB"

The docs do say "The * character is not recognized as a metacharacter within a replacement pattern"

Why? And is there any work around if you want a bit of your regex to slurp up some left over string which may not be there (like ".*?" on the end)

(this is a silly example, but you get the point)

like image 613
didster Avatar asked Feb 10 '12 12:02

didster


1 Answers

Start your pattern with ^ and end it with $ and your problem is solved.

string s = Regex.Replace("AAAA", @"^\w*$", "B");
Console.Write(s);

Alternatively - you can stop matching on 0 length strings with the + operator instead of the * operator:

string s = Regex.Replace("AAAA", @"\w+", "B");
Console.Write(s);
like image 113
Matt Fellows Avatar answered Oct 06 '22 00:10

Matt Fellows