Extract until unquote or EOL

Discussion in 'Perl Misc' started by Mats, Jul 18, 2005.

  1. Mats

    Mats Guest

    Hi!

    I've messed with this problem a while now. I want to parse a file for
    declarations (ex: NAME = "myname").

    I wan't to extract the phrase/text between the two quotes. BUT If the
    last quote isn't available (type/user error) then it should extract
    until end of line. If no quotes are there at all, it should extract the
    whole line (except NAME=). If there are several double quotes, it should
    extract between the first two (that i seem to have achived).

    My current testscript looks as below:

    ---
    #!/usr/bin/perl -w

    use strict;

    $_ = 'NAME = "between quotes" not this" nor this';

    print $1."\n" if m/\s*NAME\s*=\s*"*(.*?)"|$/s;
    ---

    This prints out as i want:
    between quotes

    But if i delete the last two doublequotes and just keep the first it
    prints nothing. If i delete the first doublequote also, i get an
    "uninitialized value" error.

    Anybody knows a smooth solution to this?

    Mats
    Mats, Jul 18, 2005
    #1
    1. Advertising

  2. Mats wrote:
    >
    > I've messed with this problem a while now. I want to parse a file for
    > declarations (ex: NAME = "myname").
    >
    > I wan't to extract the phrase/text between the two quotes. BUT If the
    > last quote isn't available (type/user error) then it should extract
    > until end of line. If no quotes are there at all, it should extract the
    > whole line (except NAME=). If there are several double quotes, it should
    > extract between the first two (that i seem to have achived).
    >
    > My current testscript looks as below:
    >
    > ---
    > #!/usr/bin/perl -w
    >
    > use strict;
    >
    > $_ = 'NAME = "between quotes" not this" nor this';
    >
    > print $1."\n" if m/\s*NAME\s*=\s*"*(.*?)"|$/s;
    > ---
    >
    > This prints out as i want:
    > between quotes
    >
    > But if i delete the last two doublequotes and just keep the first it
    > prints nothing. If i delete the first doublequote also, i get an
    > "uninitialized value" error.
    >
    > Anybody knows a smooth solution to this?


    print "$1\n" if /\s*NAME\s*=\s*"?([^"]+)/;



    John
    John W. Krahn, Jul 18, 2005
    #2
    1. Advertising

  3. Gunnar Hjalmarsson, Jul 18, 2005
    #3
  4. Mats <> wrote in
    news:jlSCe.29331$:

    > I wan't to extract the phrase/text between the two quotes. BUT If the
    > last quote isn't available (type/user error) then it should extract
    > until end of line. If no quotes are there at all, it should extract
    > the whole line (except NAME=). If there are several double quotes, it
    > should extract between the first two (that i seem to have achived).


    ....

    > print $1."\n" if m/\s*NAME\s*=\s*"*(.*?)"|$/s;


    Usually, I find it easier to deal with a literal translation of the
    requirements into the relevant index and substr calls:

    #!/usr/bin/perl

    use strict;
    use warnings;

    while(<DATA>) {
    if( /^\s*NAME\s*=\s*(.*)/ ) {
    my $v;
    if( (my $i = 1 + index $1, q{"}) ) {
    if( -1 < (my $j = index substr($1, $i), q{"}) ) {
    $v = substr $1, $i, $j;
    } else {
    $v = substr $1, $i;
    }
    } else {
    $v = $1;
    }
    print "$v\n";
    }
    }

    __DATA__
    NAME = "between quotes" not this nor this
    NAME = no quotation marks so grab all of this
    NAME = "solitary quotation mark at the beginning of line, so grab all

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Jul 18, 2005
    #4
  5. Mats

    Mats Guest

    John W. Krahn wrote:
    > Mats wrote:
    >
    >>I've messed with this problem a while now. I want to parse a file for
    >>declarations (ex: NAME = "myname").
    >>
    >>I wan't to extract the phrase/text between the two quotes. BUT If the
    >>last quote isn't available (type/user error) then it should extract
    >>until end of line. If no quotes are there at all, it should extract the
    >>whole line (except NAME=). If there are several double quotes, it should
    >>extract between the first two (that i seem to have achived).
    >>
    >>My current testscript looks as below:
    >>
    >>---
    >>#!/usr/bin/perl -w
    >>
    >>use strict;
    >>
    >>$_ = 'NAME = "between quotes" not this" nor this';
    >>
    >>print $1."\n" if m/\s*NAME\s*=\s*"*(.*?)"|$/s;
    >>---
    >>
    >>This prints out as i want:
    >>between quotes
    >>
    >>But if i delete the last two doublequotes and just keep the first it
    >>prints nothing. If i delete the first doublequote also, i get an
    >>"uninitialized value" error.
    >>
    >>Anybody knows a smooth solution to this?

    >
    >
    > print "$1\n" if /\s*NAME\s*=\s*"?([^"]+)/;
    >
    >
    >
    > John


    Well! Thats a lot less complicated and smarter than i ever thought off
    and as bonus it works! I really should be thinking in a more KISS like way.

    Thanks!

    Mats
    Mats, Jul 18, 2005
    #5
    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. William Tasso

    quote, unquote

    William Tasso, Nov 11, 2003, in forum: HTML
    Replies:
    5
    Views:
    412
    George Self
    Nov 12, 2003
  2. George Sakkis

    urllib.unquote and unicode

    George Sakkis, Dec 19, 2006, in forum: Python
    Replies:
    11
    Views:
    1,128
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Dec 22, 2006
  3. koara

    urllib.unquote + unicode

    koara, Nov 13, 2007, in forum: Python
    Replies:
    1
    Views:
    620
    Gabriel Genellina
    Nov 14, 2007
  4. Maciej Bliziñski

    urllib2.unquote() vs unicode

    Maciej Bliziñski, Mar 18, 2008, in forum: Python
    Replies:
    1
    Views:
    762
    Gabriel Genellina
    Mar 18, 2008
  5. Guest
    Replies:
    4
    Views:
    282
    Guest
    May 12, 2006
Loading...

Share This Page