Regex: How to ignore pairs of quotes? (beginner level question)

Discussion in 'Perl Misc' started by Tobias Weihmann, Apr 23, 2004.

  1. Hi,

    I'm trying to parse VB strings here but I didn't succeed in getting
    the regex to ignore pairs of double quotes (which are the equivalent to
    \" in Perl)

    Example:

    print "This is a test ""Test"" yeah" + Str$(4)+ "good"
    --> This is a test ""Test"" yeah
    --> good

    print """We""are""testing"""+"What they can do" '
    --> ""We""are""testing""
    --> What they can do

    I finished off writing a FSM, but this is quite clumsy compared to a
    regex. The best thing I had was like /"(.*?)"[^"]/, but this would get me
    --> This is a test "
    for the first example... ( the "T satisfies the end condition, even though
    the two quotes should be ignored altogether)

    I also considered /"(.*?[^"])"[^"]/, but this will break in the second
    example - because it is's of course possible to have two quotes (to be
    ignored) right before a quote finishing of the string. I also experi-
    mented with non-moving sequences (?!"") but to no avail...

    How would you do it?

    Cheers!
    Toby
     
    Tobias Weihmann, Apr 23, 2004
    #1
    1. Advertising

  2. Tobias Weihmann <> wrote:
    > Hi,
    >
    > I'm trying to parse VB strings here but I didn't succeed in getting
    > the regex to ignore pairs of double quotes (which are the equivalent to
    > \" in Perl)
    >
    > Example:
    >
    > print "This is a test ""Test"" yeah" + Str$(4)+ "good"
    > --> This is a test ""Test"" yeah
    > --> good
    >
    > print """We""are""testing"""+"What they can do" '
    > --> ""We""are""testing""
    > --> What they can do
    >
    > I finished off writing a FSM, but this is quite clumsy compared to a
    > regex. The best thing I had was like /"(.*?)"[^"]/, but this would get me
    > --> This is a test "
    > for the first example... ( the "T satisfies the end condition, even though
    > the two quotes should be ignored altogether)
    >
    > I also considered /"(.*?[^"])"[^"]/, but this will break in the second
    > example - because it is's of course possible to have two quotes (to be
    > ignored) right before a quote finishing of the string. I also experi-
    > mented with non-moving sequences (?!"") but to no avail...
    >
    > How would you do it?



    print "$1\n" while /"(([^"]|"")*)"/g;

    or, if you want to be able to read what you've written:

    print "$1\n" while /"(
    ( [^"] | "" )*
    )"
    /gx;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 23, 2004
    #2
    1. Advertising

  3. Tobias Weihmann () wrote:
    : Hi,

    : I'm trying to parse VB strings here but I didn't succeed in getting
    : the regex to ignore pairs of double quotes (which are the equivalent to
    : \" in Perl)

    : Example:

    : print "This is a test ""Test"" yeah" + Str$(4)+ "good"
    : --> This is a test ""Test"" yeah
    : --> good

    : print """We""are""testing"""+"What they can do" '
    : --> ""We""are""testing""
    : --> What they can do

    : I finished off writing a FSM, but this is quite clumsy compared to a
    : regex. The best thing I had was like /"(.*?)"[^"]/, but this would get me
    : --> This is a test "
    : for the first example... ( the "T satisfies the end condition, even though
    : the two quotes should be ignored altogether)

    : I also considered /"(.*?[^"])"[^"]/, but this will break in the second
    : example - because it is's of course possible to have two quotes (to be
    : ignored) right before a quote finishing of the string. I also experi-
    : mented with non-moving sequences (?!"") but to no avail...

    : How would you do it?

    Someone else posted something like the following to a similar problem.

    my example is tested, but just for the one example


    # $s contains the VB string you wish to parse.

    $s=q{print "This is a test ""Test"" yeah" + Str$(4)+ "good"};

    # extract the quoted portions
    @s= $s=~ m/"((?:[^"]|"")*)"/g;

    # examine what we extracted
    print map {"=>$_<=\n" } @s;
     
    Malcolm Dew-Jones, Apr 23, 2004
    #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. pabbu
    Replies:
    8
    Views:
    746
    Marc Boyer
    Nov 7, 2005
  2. Scooter

    Split string but ignore quotes

    Scooter, Sep 29, 2009, in forum: Python
    Replies:
    5
    Views:
    1,748
    Processor-Dev1l
    Sep 30, 2009
  3. Jonno
    Replies:
    0
    Views:
    210
    Jonno
    Apr 13, 2011
  4. Rob Meade
    Replies:
    6
    Views:
    283
    Rob Meade
    Mar 1, 2004
  5. jOhn
    Replies:
    1
    Views:
    237
    Phlip
    Jan 29, 2008
Loading...

Share This Page