0

How does the => operator work and how does m come into existence in the following code ?:

using System;
using System.IO;
using System.Text.RegularExpressions;
 
public class Test
{
    public static void Main()
    {
        var line = "Keeping the line intact, replace me, but not you, keep this too.\n\nRetaining me, replace one, but not two, ignore three.";
        var res = Regex.Replace(line, @"replace (?<target>.*), but not (?<source>.*),", m => "replace " + m.Groups["source"].Value.Replace("o", "1") + ", but not " + m.Groups["source"].Value + ",");
        Console.WriteLine(res);
    }
}

See the online compilation & output here.

EDIT:
This question is a duplicate but it should remain visible to searches because nowhere on StackOveflow is possible to find this association by searching for: => or for its English expansion: equals greater. The first duplicate questions refers to it as arrow which caused my searches to return - NO HITS.
An "arrow" is e.g.: (U+02192) , not the two characters =>

EDIT EDIT:
Never mind, the first duplicate question's title has been edited and now the search finds it with the search terms => or its English expansion "equal greater".

  • 3
    It's not an "equals greater" operator, but part of a lambda expression – QBrute Dec 06 '22 at 09:29
  • OK, if it is a Lambda, then I can read up on it some more. Nowhere on StackOveflow could I find this hint by searching for: `=>` or for its English expansion: `equals greater`, the duplicated questions referred to it as `arrow` which caused my searches to return - NO HITS. To me an "arrow" is U+02192 – Pavel Stepanek Dec 06 '22 at 09:33
  • => is called the lambda operator in C# and other languages. You should be able to search for that. – Palle Due Dec 06 '22 at 09:45
  • @Palle Due: Yes, now that I know how it is called, I can get a lot of search hits and I am not stuck anymore. – Pavel Stepanek Dec 06 '22 at 10:08
  • I still do not know what `m` is and where it is coming from. Most importantly: is it a name that is made up by the author of the `Main()` method or is it defined by the author of the `Replace()` method ? – Pavel Stepanek Dec 06 '22 at 10:11
  • take a look at [Regex.Replace](https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.replace?view=net-7.0#system-text-regularexpressions-regex-replace(system-string-system-string-system-text-regularexpressions-matchevaluator)) method definition and at the type of the third parameter [MatchEvaluator](https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.matchevaluator?view=net-7.0). MatchEvaluator is a delegate (a function signature) which is a function that accepts Match as argument and returns a string. – theemee Dec 06 '22 at 11:05
  • 1
    You then pass a function as a parameter to Regex.Replace. The function in the code is defined as lambda: `m` is the Match parameter of MatchEvaluator delegate. Regex.Replace then will call this delegate and provide arguments for it. Which means that Regex.Replace provides the `Match m` argument. You can use any name instead of `m` as it's you who define the function and the does not matter for the caller (which is Regex.Replace), only the type matters for the caller – theemee Dec 06 '22 at 11:09
  • So basically what it means is that Regex.Replace finds matches and then provides you with the Match object that contains those matches and you can use them to construct your replace string – theemee Dec 06 '22 at 11:12
  • @theemee: That exhaustively explains where the `m`'s name and contents come from. Thanks! – Pavel Stepanek Dec 06 '22 at 13:41
  • should I post it as an answer so we mark the question as answered and closed? – theemee Dec 06 '22 at 13:56
  • @theeme: I will accept it as an answer if you do ...but won't this question be deleted after it is closed ? – Pavel Stepanek Dec 06 '22 at 14:08
  • 2
    It's not unique to C# that either `=>` or `->` may be referred to as an arrow operator, and I don't think many people would automatically assume that `equals greater` must be the most appropriate name to give that combination. – Damien_The_Unbeliever Dec 06 '22 at 14:09
  • @Damien: `equals ... greater ...` is **NOT a naming** of the entire `=>` sequence, it is an English naming of the **individual characters** in this sequence. – Pavel Stepanek Dec 06 '22 at 14:32

0 Answers0