Matching quoted strings

T

TonyShirt

After an exaustive search on google groups I decided to post. I have
a line:

"FileVersion", "1, 2, 4, 17\0"

I want to match
FileVersion
1, 2, 4, 17\0

I tried $_ =~ /"(.*?)"/; but it only matches FileVersion. How do I
go on a match the other quoted string?
 
G

gnari

TonyShirt said:
After an exaustive search on google groups I decided to post. I have
a line:

"FileVersion", "1, 2, 4, 17\0"

I want to match
FileVersion
1, 2, 4, 17\0

I tried $_ =~ /"(.*?)"/; but it only matches FileVersion. How do I
go on a match the other quoted string?

this is csv-like. if you are parsing csv you might look at CVS modules
that know how to deal with thins like quotes/linefeeds in the strings.

if you just want what you say, you might look at the g modifier
like in my ($x,$y)=/"(.*)"/g;
you might also consider my ($x,$y)=/"(.*?)","(.*?)"/ if you prefer

gnari
 
T

Trent Curry

TonyShirt said:
After an exaustive search on google groups I decided to post. I have
a line:

"FileVersion", "1, 2, 4, 17\0"

I want to match
FileVersion
1, 2, 4, 17\0

I tried $_ =~ /"(.*?)"/; but it only matches FileVersion. How do I
go on a match the other quoted string?

Is this what you were looking for?

___Code___
#!/usr/bin/perl -w

use strict;

$_ = qq|"FileVersion", "1, 2, 4, 17\0"|;

# Note that $_, being the default, is implied here.
my @matches = /"(.*?)"/g; # Note the 'g' at the end.

print '[', join("]\n[", @matches), "]\n";

___Output___
[FileVersion]
[1, 2, 4, 17]

--
Trent Curry

perl -e
'($s=qq/e29716770256864702379602c6275605/)=~s!([0-9a-f]{2})!pack("h2",$1
)!eg;print(reverse("$s")."\n");'
 
M

Malcolm Dew-Jones

TonyShirt ([email protected]) wrote:
: After an exaustive search on google groups I decided to post. I have
: a line:

: "FileVersion", "1, 2, 4, 17\0"

: I want to match
: FileVersion
: 1, 2, 4, 17\0

: I tried $_ =~ /"(.*?)"/; but it only matches FileVersion. How do I
: go on a match the other quoted string?

That depends on what you really mean to do.

Do you wish to get the second quoted string? How do you identify the
second one? Are there two quoted string always? perhaps

/".*?".*?"(.*)"/

Is the second half always after the first comma?

/,\s*"(.*)"/

Do you actually want both (and there's always two of them), perhaps

my @all = /"(.*?)"/g

If you know the complete format then I would be tempted to parse all of
exactly what I expected or warn myself

my ($version,$numbers)
= m/^"(\w+)", "([\d, \\]+)"$/ or die "unexpected format: $_";


(All re's were untested of course.)
 
B

Bob Walton

TonyShirt said:
After an exaustive search on google groups I decided to post. I have
a line:

"FileVersion", "1, 2, 4, 17\0"

I want to match
FileVersion
1, 2, 4, 17\0

I tried $_ =~ /"(.*?)"/; but it only matches FileVersion. How do I

$-----------------------^
Try inserting a $ as the last character of the regexp. That will force
it to match a pair of quotes that terminates at the end of the string.
But while you're at it, you'd probably better also replace .*? with
something like [^"]* so that you don't match any "'s other than the ones
you intend.

go on a match the other quoted string?

Or you could try adding the /g switch to the end and saving

your results in an array. Maybe like:

@results = $_ =~ /"(.*?)"/g;

HTH.
 
B

Ben Morrow

TonyShirt said:
After an exaustive search on google groups I decided to post. I have
a line:

"FileVersion", "1, 2, 4, 17\0"

I want to match
FileVersion
1, 2, 4, 17\0

I tried $_ =~ /"(.*?)"/; but it only matches FileVersion. How do I

$-----------------------^
Try inserting a $ as the last character of the regexp. That will force
it to match a pair of quotes that terminates at the end of the string.
But while you're at it, you'd probably better also replace .*? with
something like [^"]* so that you don't match any "'s other than the ones
you intend.

That is the point of the *? minimal matching: not to match any "s other
than those intended.

Ben
 
B

Bob Walton

Ben said:
TonyShirt wrote:

After an exaustive search on google groups I decided to post. I have
a line:

"FileVersion", "1, 2, 4, 17\0"

I want to match
FileVersion
1, 2, 4, 17\0

I tried $_ =~ /"(.*?)"/; but it only matches FileVersion. How do I
$-----------------------^
Try inserting a $ as the last character of the regexp. That will force
it to match a pair of quotes that terminates at the end of the string.
But while you're at it, you'd probably better also replace .*? with
something like [^"]* so that you don't match any "'s other than the ones
you intend.

That is the point of the *? minimal matching: not to match any "s other
than those intended.


Yes, but /"(.*?)$"/ will match the entirity of the OP's string,
regardless of the lack of greediness of the .*? .
 
B

Ben Morrow

Yes, but /"(.*?)$"/ will match the entirity of the OP's string,
regardless of the lack of greediness of the .*? .

Err... probably not why you think it does, though. This interpolates $",
normally blank.

Ben
 
B

Bob Walton

Ben said:
Err... probably not why you think it does, though. This interpolates $",
normally blank.


Oh, sorry: /"(.*?)"$/ is what I meant. *That* one will match the
entirety of the OP's string.
 
T

Trent Curry

Bob said:
Oh, sorry: /"(.*?)"$/ is what I meant. *That* one will match the
entirety of the OP's string.

I understood he wanted to match each (both) quoted segments.

In other words I assumed
"FileVersion", "1, 2, 4, 17\0"
was meant to be in a string, like
'"FileVersion", "1, 2, 4, 17\0"'
or
qq|"FileVersion", "1, 2, 4, 17\0"|;

and split to
FileVersion
and
1, 2, 4, 17\0

in which case this should work, assuming there are no and never will be
escaped "'s (ie: \" ) in each "..." (mini) string in the whole outter
qq|...| string:

$_ = qq|"FileVersion", "1, 2, 4, 17\0"|;

my @matches = /"(.*?)"/g;
# or perhaps: /"(.*?)",?\s*/g

Maybe I over looked something?

--
Trent Curry

perl -e
'($s=qq/e29716770256864702379602c6275605/)=~s!([0-9a-f]{2})!pack("h2",$1
)!eg;print(reverse("$s")."\n");'
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top