Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: matching a line with ' and "

by hipowls (Curate)
on Jul 18, 2008 at 08:57 UTC ( #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}"; }


Comment on Re: matching a line with ' and "
Select or Download Code
Re^2: matching a line with ' and "
by ww (Bishop) 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
Node Status?
node history
Node Type: note [id://698542]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (17)
As of 2015-07-02 18:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (44 votes), past polls