Regex that matches a newline (\n) in C#
If you specify RegexOptions.Multiline then you can use ^
and $
to match the start and end of a line, respectively.
If you don't wish to use this option, remember that a new line may be any one of the following: \n
, \r
, \r\n
, so instead of looking only for \n
, you should perhaps use something like: [\n\r]+
, or more exactly: (\n|\r|\r\n)
.
Why does regex detect newline as a valid newline but not \n?
from what Tim Biegeleisen said, "on windows the line separator is not just \n, it's \r\n", with this I changed pattern 2 to \d{1,}\r\n\w
and everything turned out fine.
Replace a new line char with regex in C#
In Windows, line breaks usually look like \r\n
(caret return + line feed).
So, you can match linebreaks that are preceded and followed by an alphanumeric with
string output = Regex.Replace(pathFOSE, @"(?<=\w)\r\n(?=\w)", " ");
Mind that \w
matches Unicode letters and an underscore, too. If you do not need that behavior (and only need to match English letters) use
string output = Regex.Replace(pathFOSE, @"(?i)(?<=[a-z0-9])\r\n(?=[a-z0-9])", " ");
If you have a mixture of line breaks from various OSes or programs, you may use
string output = Regex.Replace(pathFOSE, @"(?i)(?<=[a-z0-9])(?:\r\n|\n|\r)(?=[a-z0-9])", " ");
And in case there are multiple line breaks, add a +
quantifier (?:\r\n|\n|\r)+
.
To perform search and replace on the file contents, you need to read the file in.
You can do it with
var pathFOSE = @"D:\Public\temp\FOSEtest.txt";
var contents = File.ReadAllText(pathFOSE);
var output = Regex.Replace(contents, @"(?i)(?<=[a-z0-9])(?:\r\n|\n|\r)(?=[a-z0-9])", " ");
var pathNewFOSE = @"D:\Public\temp\NewFOSE.txt";
if (!System.IO.File.Exists(pathNewFOSE))
{
File.WriteAllText(pathNewFOSE, output);
}
How do I match any character across multiple lines in a regular expression?
It depends on the language, but there should be a modifier that you can add to the regex pattern. In PHP it is:
/(.*)<FooBar>/s
The s at the end causes the dot to match all characters including newlines.
Regex replace multiple new lines
The point is that your regex matches sequences of \r\n
(2 or more) and then 2 or more sequences of \r\r
. You need
[\r\n]+
Or [\r\n]{2,}
if you need to only match 2 or more occurrences of \r
or \n
.
If you need to exactly match 2 or more common line break types (\r\n
in Windows, \n
in Unix/Linux and \r
on Mac OS), use
(?:\r?\n|\r){2,}
Related Topics
Escape Quote in C# for JavaScript Consumption
Preventing Jit Inlining on a Method
C# Encryption to PHP Decryption
Show Authentication Dialog in C# for Windows Vista/7
Internal .Net Framework Data Provider Error 1025
Openxml Sdk Having Borders for Cell
How to Draw a Rounded Rectangle with Winforms (.Net)
Why Doesn't the C# Ternary Operator Work with Delegates
No Itemchecked Event in a Checkedlistbox
How to Get a Unique Identifier for a Device Within Windows 10 Universal
System.Io.Filestream Fileaccess VS Fileshare
Topmost Form, Clicking "Through" Possible
Open File in Exclusive Mode in C#