Say I want to use a regular expression to add "test" before each word in a string.
string MyText="hello world"
string Pattern = "\w+";
I could do this:
Regex.Replace(MyText, Pattern, "test$&")
or this:
Regex.Replace(MyText, Pattern, m=>"test"+m.Value)
I would get the same result so what's the difference between $& and Value in the lambda expression? If there's not difference in terms of results, is there a performance issue?
Lambdas are anonymous methods which work just the same as regular ones. The one declared in your example is equivalent to:
string Convert(RegexMatch match) { return "test" + match.Value; }
.
Using this syntax can give you access to a much richer range of possibilities then using the Regex expression used in your first example.
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