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("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


First line


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?


$ 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")]
>>> [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]

Need Your Help

System.Web.Optimization.JsMinify produces invalid JavaScript minify bundling-and-minification webgrease

I am using System.Web.Optimization v1.3 in what I believe is a standard configuration, to bundle and minify the JavaScript and CSS files in an MVC 5 web application. For the most part, this works v...

PHP to Detect and Split Out html Special Character Codes in a String?

php unicode utf-8 hex special-characters

In PHP when i read the Data, lets say the data (chunk of string) is containing HTML Special Character DECIMAL HEX Codes like:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.