Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
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 avoiding work at the Monastery: (7)
As of 2014-12-25 19:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (162 votes), past polls