Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find a carriage return without a line feed after it with regex pattern?

Tags:

c#

regex

I need to find a carriage return (\r) that doesn't have a line feed (\n) directly after it how would I do this with a regex pattern?

like image 474
Joey Gfd Avatar asked Aug 08 '11 18:08

Joey Gfd


People also ask

What is RegEx for carriage return?

You can use special character sequences to put non-printable characters in your regular expression. Use \t to match a tab character (ASCII 0x09), \r for carriage return (0x0D) and \n for line feed (0x0A).

What is \r and \n in RegEx?

\n. Matches a newline character. \r. Matches a carriage return character.

How do you match line breaks in RegEx?

If you want to indicate a line break when you construct your RegEx, use the sequence “\r\n”. Whether or not you will have line breaks in your expression depends on what you are trying to match. Line breaks can be useful “anchors” that define where some pattern occurs in relation to the beginning or end of a line.

How do you match everything including newline RegEx?

The dot matches all except newlines (\r\n). So use \s\S, which will match ALL characters.


2 Answers

What about the following regex with a negative lookahead:

\r(?!\n)
like image 110
Howard Avatar answered Oct 06 '22 00:10

Howard


This should do the trick:

 Regex.Match("\rtext\r\ntext.", "\r[^\n]", RegexOptions.Multiline);
like image 36
Ryan Gross Avatar answered Oct 06 '22 01:10

Ryan Gross