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)
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With