Regular expression to replace "\ r \ n" only if not preceded by ".". in line

Could you provide a regex that I can use to replace all \r\n in a string only when \r\n not preceded . ?

+4
source share
2 answers

To match a character, you can put the character inside the brackets, for example [.] . To not match it, you can start the list of characters with a caret such as [^.] . This will effectively match any character that is not . .

In your particular case, you want to match \r\n which does not . in front of him. In combination with the above, you can use:

 [^.]\r\n 

To replace it, you will want to β€œcapture” a symbol that is not a period in order to keep it in the replacement. You can write it by enclosing it in parentheses, for example ([^.]) .

Using Regex.Replace() , it will be something like this:

 yourString = Regex.Replace(yourString, @"([^.])\r\n", "$1"); 

$1 matches the character and is replaced with a string again, now deprived of \r\n .

+7
source

I think it will work

 Regex.Replace(input, @"([^.]?)\r\n", "$1"); 
+2
source

All Articles