PDA

Click to See Complete Forum and Search --> : the @ symbol in regular expressions


r0k3t
Jun 12th, 2007, 11:02 AM
This might sound like a dumb question but I haven't seen this anywhere. I noticed that a lot of examples have the @ before the regex, why is that?
I haven't been using it and it is fine. My regex just look like this

RegEx tableOpenRegex = new RegEx("<table");

works fine - what am I missing...

Thanks

penagate
Jun 12th, 2007, 12:07 PM
The '@' symbol prefixes a verbatim string literal. It is used to indicate that you do not wish escape sequences in the string to be parsed (all except for \"). This is useful in regular expressions because they use the same escape character as C# does ('\'). If the verbatim literal form is not used, you need to escape the escape characters, which results in an expression that is hard to read and difficult to port.

mendhak
Jun 12th, 2007, 02:27 PM
If you have the regular expression

123\.aspx(?!\.)

You'd usually say

string regEx = "123\\.aspx(?!\\.)";

But it'd be easier to read if you said

string regEx = @"123\.aspx(?!\.)";

Also, it's useful when you want a very readable large string.


string vbfFooter = @" Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info

Copyright 2007 Jupitermedia Corporation All Rights Reserved.
Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers";

r0k3t
Jun 13th, 2007, 07:52 AM
Ahh - thanks. I just read a article about it online not to long ago and they never even mentioned it - or I missed it... My regex's where pretty simple so it was never a problem.