Regular expression matches full lines of text excluding crlf

What will the regular expression pattern look like for each line of a given text?

I'm trying ^(.+)$ , But it includes crlf ...

+4
source share
5 answers

I'm not sure what you mean by “matching every line of a given text,” but you can use a character class to exclude CR and LF characters:

 [^\r\n]+ 
+3
source

Just use RegexOptions.Multiline .

Multiline mode. Changes the value of ^ and $, so they coincide at the beginning and end, respectively, of any line, and not just the beginning and end of the entire line.

Example:

 var lineMatches = Regex.Matches("Multi\r\nlines", "^(.+)$", RegexOptions.Multiline); 
+3
source

You tried:

 ^(.+)\r?\n$ 

Thus, the match group includes everything except CRLF and requires a new line (default is Unix), but accepts a carriage return in front (default is Windows).

0
source

I assume you are using the Multiline parameter? In this case, you need to explicitly match the new line with "\ n". (replace "\ r \ n" as appropriate.)

0
source

The wording of your question seems a bit unclear, but it looks like you want RegexOptions.Multiline (in the System.Text.RegularExpressions namespace). This is an option that you should install on your RegEx object. This should make ^ and $ match the beginning and end of the line, not the entire line.

For instance:

 Regex re = new Regex("^(.+)$", RegexOptions.Compiled | RegexOptions.Multiline); 
0
source

All Articles