Regular Expressions

Discussion in 'Python' started by Ron, Mar 23, 2005.

  1. Ron

    Ron Guest

    This is probably a repeated question, but try as I might I was unable
    to find something similar in the archives.

    I'm trying to develop a regular expression for recognizing a simplified
    C-Style string syntax. I need it to be able to handle escape sequences
    of the form \x where x is any character including ".

    Here's what I'm trying:

    \"([^"\\]|(\\.))*\"

    When I try to get it to recognize something like:

    "I said, \"Hello!\""

    It stops at the first quote after the \.

    I've used this very same regular expression in a parser generator I
    have for C++ and it works just fine.

    Any thoughts on what I'm doing wrong in the Python Reg Ex world?

    Thanks for the comments & help.

    Ron
     
    Ron, Mar 23, 2005
    #1
    1. Advertising

  2. Ron

    MyHaz Guest

    MyHaz, Mar 23, 2005
    #2
    1. Advertising

  3. Ron wrote:
    > This is probably a repeated question, but try as I might I was unable
    > to find something similar in the archives.
    >
    > I'm trying to develop a regular expression for recognizing a simplified
    > C-Style string syntax. I need it to be able to handle escape sequences
    > of the form \x where x is any character including ".
    >
    > Here's what I'm trying:
    >
    > \"([^"\\]|(\\.))*\"
    >
    > When I try to get it to recognize something like:
    >
    > "I said, \"Hello!\""
    >
    > It stops at the first quote after the \.


    Works for me:

    >>> print re.search(r'\"([^"\\]|(\\.))*\"',

    .... r'"I said \"Hello!\""').group(0)
    "I said \"Hello!\""


    You can leave out the backslashes in fron of the first and last quotes
    in the regex, by the way, at least if you use ' instead of " to delimite it:

    >>> print re.search(r'"([^"\\]|(\\.))*"',

    .... r'"I said \"Hello!\""').group(0)
    "I said \"Hello!\""


    --
    If I have been able to see further, it was only because I stood
    on the shoulders of giants. -- Isaac Newton

    Roel Schroeven
     
    Roel Schroeven, Mar 23, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay Douglas

    Custom Regular Expressions in ASP.net

    Jay Douglas, Nov 2, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    617
    mikeb
    Nov 3, 2003
  2. mark

    Regular expressions

    mark, Jun 30, 2003, in forum: Perl
    Replies:
    4
    Views:
    1,735
  3. Dustin D.
    Replies:
    1
    Views:
    11,372
  4. Jay Douglas
    Replies:
    0
    Views:
    635
    Jay Douglas
    Aug 15, 2003
  5. Noman Shapiro
    Replies:
    0
    Views:
    254
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page