• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1077
  • Last Modified:

Catching \n\r with RegEx

I have the following multi-line text:

</b><br></span>
</td></tr><tr><td align="left"

There is a hex 0d0a after span> and before </td>

This is my Regex expression (which is not working)...

r2 = new Regex(@"</b><br></span>\\r\\n</td></tr><tr><td align=""left""");

Can someone please tell me why it won't recognize the CRLF?  I've tried it with \\n\\r also (I'm a bit confused as to what the 0d0a means as far as /n and /r)

Thank you in advance... :D
0
rmmarsh
Asked:
rmmarsh
  • 6
  • 2
1 Solution
 
AGBrownCommented:
Could you try [\\r\\n]+? I've got that in my regexs and it seems to work. Of course you can then narrow it down with a range {1, 2} instead of + later on.

Andy
0
 
AGBrownCommented:
Looking again - I think you might also be having problem with the < and > characters - can you try escaping those to \\< and \\>

What are you using the regex in? Is it with a regex object in code?
0
 
AGBrownCommented:
lol: when I say "Could you try [\\r\\n]+?" I don't mean you should include the question mark - sorry!

And you can forget my last "I'm with stupid" question about "is it with ..."

I think you should try:

r2 = new Regex(@"\</b\>\<br\>\</span\>[\r\n]+\</td\>\</t\d>\<tr\>\<td align=""left""");

Note that if you are using @ before your string, you don't need to escape the backslashes.

Andy
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AGBrownCommented:
ugh, my typing sucks:

r2 = new Regex(@"\</b\>\<br\>\</span\>[\r\n]+\</td\>\</td\>\<tr\>\<td align=""left""");

(corrected "/t\d>" to "/td\>")

Sorry for the email spam :-)
0
 
rmmarshAuthor Commented:
Normally I have no problems with the < and >... it's just that this is multi-line text and the /n/r is giving me a fit... don't know if /n is a 0x0a or a 0x0d and that's part of the problem... the other part of the problem is that the RegEx expression is wrong, but I can't see it...

Tried both of your ideas, and it still fails... again, I don't think it's the < or >

:D
0
 
bruintjeCommented:
Hello rmmarsh,

tried your sample html from above in a richtextbox and some code like

string Msg = richTextBox1.Text;  

//when i looked at Msg here it showed a \n as linefeed character

// and after some experimentation this expression matched the above
Match expr = Regex.Match(Msg, @"</b><br></span>\n</td></tr><tr><td align=""left""");

MessageBox.Show(expr.Value);

so i don't know if this resembles your setup but i didn't need to escape \n so it could work for you with a clean \r\n

hope this helps a bit
bruintje
0
 
rmmarshAuthor Commented:
Thank you so much, bruintje... that worked...

0
 
AGBrownCommented:
Hmm, sorry about that. How are you testing your regex? Have you tried developing it in something like Expresso (http://www.ultrapico.com/Expresso.htm) and then moving it into code?

AFAIK, \r is a carriage return, which is ascii 13, and so is 0x0d, and \n is a line feed character, so is ascii 10, which is 0x0d. So you are definately using the right characters.

You are right about the < and > I don't know where I got that particular sock to smoke (all of my regex just became a lot simpler so thank _you_).

If i do simply:
r2 = new Regex(@"</b><br></span>\r\n</td></tr><tr><td align=""left""");
with the regex options
Ignore case, MultiLine (no others) then it succesfully captures text from a text file with your text in.

Can you try
r2 = new Regex(@".\r\n.");
and see if it captures the ">\r\n<" part? It does in my test case.

Andy
0
 
AGBrownCommented:
Glad you got it to work, but I'm a little confused as if you have 0x0d0x0a then you definitely have \r\n and not just \n. Never mind, as long as it works in your code ...
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now