Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: matching a line with ' and "

by hipowls (Curate)
on Jul 18, 2008 at 08:57 UTC ( [id://698542]=note: print w/replies, xml ) Need Help??


in reply to matching a line with ' and "

You could use a character class and a back reference instead of alternation.

my $string = qq{var1='1' var2="2" var3="3"}; while ( $string =~ /(\w+)=(['"])(.*?)\2/g) { print "$1 = $3\n"; }
or using perl 5.10
use 5.010_000; while ( $string =~ /(?<variable>\w+)=(?<delim>['"])(?<value>.*?)\k<del +im>/g) { say "$+{variable} = $+{value}"; }

Replies are listed 'Best First'.
Re^2: matching a line with ' and "
by ww (Archbishop) on Jul 18, 2008 at 11:36 UTC

    Note that when the string includes mixed quoting which is not valid in the OP's terms:

    my $string = qq{var1='1' var2="2" var3="3" var4='4" var5="5' var6="correct"};

    ...hipowls non-5.10 version (5.10 not tested) produces "ugly" output in the sense used by almut and skeeve

    var1 = 1 var2 = 2 var3 = 3 var4 = 4" var5="5 var6 = correct

    More significantly, if $str is in the form:

    my $string = qq{var1='1' var2="2" var3="3" var4='4" var5="5'

    ...the output becomes:

    var1 = 1 var2 = 2 var3 = 3 var4 = 4" var5=

    ...which is the same as the output produced if var5 is properly quoted, var5='5':

    my $string = qq{var1='1' var2="2" var3="3" var4='4" var5='5'};

    i.e.,

    var1 = 1 var2 = 2 var3 = 3 var4 = 4" var5=

    That sort of ambiguity may be a problem for OP.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://698542]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-19 18:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found