Search code examples
c#regexend-of-line

is '$' in Regex finds break line or not in c#


When i Execute this code i got some interesting results.

  string readText = File.ReadAllText("d:\\temp.txt");
  Console.WriteLine(readText);

  Console.WriteLine("From File: "+Regex.Matches(readText,"$").Count);//-->2

  Console.WriteLine("Add \\n to the File: "+Regex.Matches(readText + "\n", "$").Count);//--->2
  Console.WriteLine("Add multiple \\n to the file: "+Regex.Matches(readText + "\n\n\n", "$").Count);//--->2

  Console.WriteLine("on Text \"sample\": "+Regex.Matches("sample", "$").Count);//--->1
  Console.WriteLine("on Text \"sample\\n\\n\\n\": "+Regex.Matches("sample" + "\n\n\n", "$").Count);//--->2

Output:

First line

third


Line 6
Line 7

From File: 2
Add \n to the File: 2
Add multiple \n to the file: 2
on Text "sample": 1
on Text "sample\n\n\n": 2

why its gives me results like this. can any one ex-plane this?


Solution

  • $ matches in two possible positions: (scroll to the section "Strings Ending with a Line Break")

    1. at the end of the input string and
    2. at the position before the last linebreak in a string if the string ends with a linebreak.

    So if your string ends with one or more newlines, you get two matches for $. In other cases, you get one.

    If you only want to match the very end of a string, use \z instead.

    An experiment in Python:

    >>> [match.start() for match in re.finditer("$", "hello")]
    [5]
    >>> [match.start() for match in re.finditer("$", "hello\n")]
    [5, 6]
    >>> [match.start() for match in re.finditer("$", "hello\n\n")]
    [6, 7]
    >>> [match.start() for match in re.finditer("$", "hello\n\n\n")]
    [7, 8]