Regex.Replace with a CRLF. Help?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi...

I'm getting stumped over the something that MUST be simple...basically, I'm
trying to replace the characters 0D0A with a crlf, using Regex.Replace().

The problem is, the replaced string includes the literal \r and \n, not a
CRLF. I've also tried Enviroment.Newline, but this gives the same effect.

Can anyone help me with this?

Thanks

Jon
 
Remember that CRLF stand for Carriage Return and Line Feed.

Consulting trusty www.asciitable.com, we see that an ascii character code of
0x0A is a new line, while 0x0D is a carriage return, just as \n and \r are
the escape characters for a new line and carriage return respectively.

If you run:

Regex.Replace("All spaces in this string will be replaced with a hard
return"," ","\r\n");

You will see all spaces replaced with \r\n in the debugger, but if you dump
them to a file or to a text output you will see the hard returns you desire.

I may be off on what I am saying as I don’t complete understand your problem.

Brendan
 
Try Regex.Replace("First Line0D0ASecond
Line","0D0A",System.Environment.NewLine);

Hope it helps,
Ludovic SOEUR.
 
I'm getting stumped over the something that MUST be simple...basically, I'm
trying to replace the characters 0D0A with a crlf, using Regex.Replace().

The problem is, the replaced string includes the literal \r and \n, not a
CRLF. I've also tried Enviroment.Newline, but this gives the same effect.

The .NET implementation of regular expressions doesn't seem to recognize
character escapes in the replacement string. The description of replacement
patterns in the VS help doesn't include character escapes so it's probably the
way it was supposed to work.

When I ran into this (trying to let a user specify replacement characters in
hex), I ended up preprocessing the replacement pattern to change, for
instance, \x0d into a binary return character.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top