By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,230 Members | 1,278 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,230 IT Pros & Developers. It's quick & easy.

Regex.Replace with a CRLF. Help?

P: n/a
Jon
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
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
"Jon" wrote:
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

Nov 17 '05 #2

P: n/a
Try Regex.Replace("First Line0D0ASecond
Line","0D0A",System.Environment.NewLine);

Hope it helps,
Ludovic SOEUR.

"Jon" <Jo*@discussions.microsoft.com> a écrit dans le message de
news:0B**********************************@microsof t.com...
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

Nov 17 '05 #3

P: n/a
In article <0B**********************************@microsoft.co m>, Jon wrote:
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

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.