Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Regular Expression Pieces

by Angel (Friar)
on May 07, 2003 at 22:06 UTC ( [id://256394]=perlquestion: print w/replies, xml ) Need Help??

Angel has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

I have tried to get this to work, I am trying to extract the pieces from a string that have the pattern [something]. I am trying to write a mail merge program and another mail merge program :) has this syntax

Ideally I thought that the $1....$n held the pieces that the regex found that match. But I am only getting the first match ( since I turned off greedy matching ) and the others are not being found.

Any ideas on what I am missing do I need to copy the string find the match delete it from the string and find again until the regex returns false?

#!/usr/local/bin/perl my $a = "this is a test [[1]] [[ a ]] [[ abd ]] [[\%object,key]]"; $a =~ m/(\[\[.*?\]\])/; print "$_, $1 , $2 , $3 , $4 , $+ \n";

What I would like out is:<\p>

"1, [[1]] , [[ a ]] , [[ abd ]] , [[\%object,key]] , 4 \n";

Replies are listed 'Best First'.
Re: Regular Expression Pieces
by dragonchild (Archbishop) on May 07, 2003 at 22:13 UTC
    I don't know how to do exactly what you trying, but i know how to get the results you want.
    my $a = "this is a test [[1]] [[ a ]] [[ abd ]] [[\%object,key]]"; my @x = $a =~ m/(\[\[.*?\]\])/g; print "@x\n";

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      How to interpret .*?
      ^_^
        ? is the modifier that says do a minimal match vs. a maximal match. Run the following code:
        my $x = "aabaab"; my ($y1) = $x =~ /([ab]+)/; my ($y2) = $x =~ /([ab]+?)/; print "$x -> '$y1' '$y2'\n";

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

(jeffa) Re: Regular Expression Pieces
by jeffa (Bishop) on May 07, 2003 at 22:14 UTC
    You need to specify the 'g' option to get all occurences, but sometimes relying on $1, etc. is overkill:
    my $a = "this is a test [[1]] [[ a ]] [[ abd ]] [[\%object,key]]"; print $_,$/ for $a =~ /(\[\[[^\]]+\]\])/g; __DATA__ [[1]] [[ a ]] [[ abd ]] [[%object,key]]
    UPDATE: Haven't you people read Death to Dot Star!? ;) Seriously, while a non-greedy dot star works does work for this example, i cannot recommend using it when there is a better solution: a negated character class ([^\]]+).

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

      If performance is not an issue, .* is usually a lot more readable...

      Wouldn't it be nice if the regex engine optimizes .*'s followed by a character(class) to a negated character class? Seems true to DWIM to me...

Re: Regular Expression Pieces
by kelan (Deacon) on May 07, 2003 at 22:21 UTC

    Because you only have one pair of parentheses, you'll only get $1 defined. A way that you can catch all your matches would be something like this:

    my @matches = ($a =~ m/(\[\[.*?\]\])/g);
    Then the array @matches will contain all of the matches and you can step through them if you want by using indices to access @matches. Please note to use the /g modifier at the end of the regex to find every possible match.

    kelan


    Perl6 Grammar Student

Re: Regular Expression Pieces
by PodMaster (Abbot) on May 07, 2003 at 22:12 UTC
    How do you figure $2 and up would be defined?
    You only use a single pair of ()'s.
    use Text::Balanced;
    use Regexp::Common::balanced;
    `perldoc perlre'


    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://256394]
Approved by dragonchild
Front-paged by dragonchild
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-23 21:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found