Results 1 to 4 of 4

Thread: the @ symbol in regular expressions

  1. #1

    Thread Starter
    Hyperactive Member r0k3t's Avatar
    Join Date
    Dec 2005
    Location
    Cleveland
    Posts
    361

    the @ symbol in regular expressions

    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

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: the @ symbol in regular expressions

    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.

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: the @ symbol in regular expressions

    If you have the regular expression

    Code:
    123\.aspx(?!\.)
    You'd usually say

    Code:
    string regEx = "123\\.aspx(?!\\.)";
    But it'd be easier to read if you said

    Code:
    string regEx = @"123\.aspx(?!\.)";
    Also, it's useful when you want a very readable large string.

    Code:
    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";

  4. #4

    Thread Starter
    Hyperactive Member r0k3t's Avatar
    Join Date
    Dec 2005
    Location
    Cleveland
    Posts
    361

    [Resolved] the @ symbol in regular expressions

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width