Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Extract until unquote or EOL (http://www.velocityreviews.com/forums/t893338-extract-until-unquote-or-eol.html)

Mats 07-18-2005 06:15 PM

Extract until unquote or EOL
 
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

John W. Krahn 07-18-2005 06:43 PM

Re: Extract until unquote or EOL
 
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

Gunnar Hjalmarsson 07-18-2005 06:53 PM

Re: Extract until unquote or EOL
 
John W. Krahn wrote:
>
> print "$1\n" if /\s*NAME\s*=\s*"?([^"]+)/;


Nice. Only that the leading \s* can be dropped.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

A. Sinan Unur 07-18-2005 06:56 PM

Re: Extract until unquote or EOL
 
Mats <spamenot.mog.pettersson@telia.com> wrote in
news:jlSCe.29331$d5.182409@newsb.telia.net:

> 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@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

Mats 07-18-2005 07:09 PM

Re: Extract until unquote or EOL
 
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


All times are GMT. The time now is 05:16 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.