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)
Regex.Replace(".,A", @"\w*", "B")becomesB.B,BBRegex.Matches("A", @"\w*").Countequal to2rather than1? And although a similar question has been asked and answered, for me the question of why is still open. After all,"A"is also 65 empty strings, followed byA, followed by 324 empty strings, so why2matches rather than390?!