Re: Extract until unquote or EOL



Mats <spamenot.mog.pettersson@xxxxxxxxx> wrote in
news:jlSCe.29331$d5.182409@xxxxxxxxxxxxxxx:

> 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 <1usa@xxxxxxxxxxxxxxxxxxx>
(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
.



Relevant Pages

  • Re: problem with spaces in quoted string arguments
    ... Janis Papanagnou wrote: ... are in double quotes. ... As can be seen in the output, the server called "photon hub" did not ... extract properly, since the space was detected in the argument to awk. ...
    (comp.unix.shell)
  • Re: Problem with Importing CSV with "=" inside a field value
    ... is a small Access database ... and have overcome a number of obstacles. ... it has an equal sign at the front and wrapped by quotes. ... Any way to skip parsing this field such that Access can extract the fields ...
    (microsoft.public.access.externaldata)
  • Re: Problem with Importing CSV with "=" inside a field value
    ... and have overcome a number of obstacles. ... it has an equal sign at the front and wrapped by quotes. ... This problematic field actually is of no use to me. ... Any way to skip parsing this field such that Access can extract the fields ...
    (microsoft.public.access.externaldata)
  • Problem with Importing CSV with "=" inside a field value
    ... and have overcome a number of obstacles. ... it has an equal sign at the front and wrapped by quotes. ... This problematic field actually is of no use to me. ... Any way to skip parsing this field such that Access can extract the fields ...
    (microsoft.public.access.externaldata)
  • Re: So what is the point of darwinports then?
    ... Most WPs call them 'smart quotes' now. ... What do the files look like in TeXShop, ... quotation marks look like proper `sexed' versions, ... printing an A5 booklet from a TeX source. ...
    (uk.comp.sys.mac)