Hi Grant,

On Sat, 16 Jun 2012 03:22:58 -0700
Grant <emailgr...@gmail.com> wrote:

> Can anyone show me how to save to a variable the contents of a set of
> double-quotes in a string?  The string could look like any of these:
> 
> phrase1 "phrase2" phrase3
> "phrase2" phrase3
> phrase1 phrase3 phrase4
> 
> In all of these examples, phrase2 would be saved to the variable.
> Many thanks to anyone who can show me how to do this.
> 

You should use a regular expression (see
http://perl-begin.org/topics/regular-expressions/ ).

Here is an example that does what you want:

[CODE]
#!/usr/bin/perl

use strict;
use warnings;

while (my $line = <>)
{
    if (my ($in_quotes) = ($line =~ /"([^"]*)"/))
    {
        print "Found match: <<$in_quotes>>\n";
    }
    else
    {
        print "Could not find match in line.\n";
    }
}
[/CODE]

And here is a shell session of interacting with it:

[SHELL]
shlomif@telaviv1:~$ perl Test.pl
Hello
Could not find match in line.
One "two" three
Found match: <<two>>
A long long time "ago", there was a princess
Found match: <<ago>>
Foobar
Could not find match in line.
[/SHELL]

( Please reply to all recipients. )

Regards,

        Shlomi Fish


-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
My Favourite FOSS - http://www.shlomifish.org/open-source/favourite/

mplayer 0.9.999.2010.03.11-rc5-adc83b19e793491b1c6ea0fd8b46cd9f32e592fc now
available for download.
    — Shlomi Fish and d3x.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to