Regex - NewLine

K

Krish

Hi,

In our application we need to replace the text \n to \r\n, but few of
our text has \r\n. I am not sure how to handle the case.

for eg; Test\rOneMore\r\n.

I need the output has Test\r\nOneMore\r\n.

I know I can do this using String.Replace method... but I need to use
Regex.

I tried using

String str = "Test\nOneMore\r\n";
Regex rx = new Regex("[^\r]\n",RegexOptions.IgnoreCase |
RegexOptions.Compiled);
str = rx.replace(str, "\r\n");

Output:
Tes\r\nOneMore\r\n. ** "t" from test is missing. I think it is
expected behaviour"

Anyhelp is appreciated.

Thanks,
Krish
 
E

eBob.com

Learn some patience. No one here is obliged to help you at all, let alone
within six hours.

It's an area I have not used extensively so I won't try to give you the
precise solution, but I think that you need to look at something called a
"zero width look behind assertion" - or something similar. These are
described under "Grouping Constructs" in msdn documentation:
http://msdn.microsoft.com/en-us/library/bs2twtah.aspx

You may have to look at the MultiLine and SingleLine options too.

If you have to experiment a bit and iterate yourself to the solution you
will find Expresso from Ultrapico (it's free) extremely helpful.

Bob


Any help!!!
 
K

Krish

I appreciate ur help!!

Your pointer is so helpful...

Regex rx1 = new Regex("<BR/?>|(?<!\r)\n", RegexOptions.IgnoreCase |
RegexOptions.Compiled);

Thanks,
Krish


Learn some patience.  No one here is obliged to help you at all, let alone
within six hours.

It's an area I have not used extensively so I won't try to give you the
precise solution, but I think that you need to look at something called a
"zero width look behind assertion" - or something similar.  These are
described under "Grouping Constructs" in msdn documentation:http://msdn.microsoft.com/en-us/library/bs2twtah.aspx

You may have to look at the MultiLine and SingleLine options too.

If you have to experiment a bit and iterate yourself to the solution you
will find Expresso from Ultrapico (it's free) extremely helpful.

Bob


Any help!!!

In our application we need to replace the text \n to \r\n, but few of
our text has \r\n. I am not sure how to handle the case.
for eg; Test\rOneMore\r\n.
I need the output has Test\r\nOneMore\r\n.
I know I can do this using String.Replace method... but I need to use
Regex.
I tried using
String str = "Test\nOneMore\r\n";
Regex rx = new Regex("[^\r]\n",RegexOptions.IgnoreCase |
RegexOptions.Compiled);
str = rx.replace(str, "\r\n");
Output:
Tes\r\nOneMore\r\n. ** "t" from test is missing. I think it is
expected behaviour"
Anyhelp is appreciated.
Thanks,
Krish- Hide quoted text -

- Show quoted text -
 

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

Top