Re: Extract until unquote or EOL
- From: "A. Sinan Unur" <1usa@xxxxxxxxxxxxxxxxxxx>
- Date: Mon, 18 Jul 2005 18:56:42 GMT
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
.
- References:
- Extract until unquote or EOL
- From: Mats
- Extract until unquote or EOL
- Prev by Date: Re: Extract until unquote or EOL
- Next by Date: Re: PAR 0.89 + Socket + ASP 5.8.7 = PL_memory_wrap
- Previous by thread: Re: Extract until unquote or EOL
- Index(es):
Relevant Pages
|