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

I dont know, this one realy struggles me...
What I am basicaly looking for is the stuff in the brackets ([[[.*]]]). So the array should have @strings = one, two, three


$string = "hereiam[[[one]]] [[[two]]][[[three]]] wwhat "; # regex print "$string\n"; @strings = split / /, $string; foreach $result (@strings){ print "\"$result\"\n"; }
Would you please be so kind?!?!?!?
Thank you very much in advance!!!

I got it! Thanks anyways!!!
while ($string =~ /\[\[[(.*?)\]]\]/ig) { print "The pattern I care about is \"$1\"\n"; }

20070911 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: Perl regex
by johngg (Canon) on Sep 06, 2007 at 09:33 UTC
    If you use a regular expression you need to be aware that open and close square brackets are metacharacters so need to be escaped to match literally. You can use regex captures and a global match to pull out the items within the brackets.

    use strict; use warnings; my $string = q{hereiam[[[one]]][[[two]]][[[three]]]wwhat}; my @strings = $string =~ m{\[+([^\]]*])\]+}g; print qq{@strings\n};

    This produces

    one two three

    To make your posts easier to read you should surround code with <code> and </code> tags.

    I hope this is of use.

    Cheers,

    JohnGG

Re: Perl regex
by Prof Vince (Friar) on Sep 06, 2007 at 10:38 UTC
    You could use Text::Balanced for that :
    use Text::Balanced qw/gen_extract_tagged/; my $text = q{hereiam[[one[[foo]]bar]]baz[[two]][[three]]wwhat}; my $parser = gen_extract_tagged "\Q[[", "\Q]]", qr/.*?(?<!\[\[)(?=\[\[)/, { bad => [ "\Q[[" ] }; while (($extracted, $remainder) = $parser->($text) and $remainder ne $text) { print "$extracted\n"; $text = $remainder; }