Extract until unquote or EOL

M

Mats

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
 
J

John W. Krahn

Mats said:
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
 
A

A. Sinan Unur

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
 
M

Mats

John said:
Mats said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top