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

I have the following patterns that I would like all to match:
${ThisOne} ${This${One}} ${This${One}Too} ${This${One}and${also}this} ...
Basically the start pattern is '${' and the end pattern is '}'. I have tried everything I know, including the folding stuff (though I didn't understand that one so well). Here's what I'm working with now (which also doesn't work):
m/ \$\{( [^\{\$\}]* (?: \$\{ [^\$\{\}]+ \} [^\$\{\}]* )* )\} /gxs;
I am matching in an entire file and the results from strings like '${somekindof${PKG}}' get the string 'PKG}' in the $1 global. I just want 'somekindof${PKG}' in the $1. Where am I going wrong? Thanks for the help monks. ArmadilloMan

Replies are listed 'Best First'.
Re: Regexp Question
by Zaxo (Archbishop) on Jun 11, 2002 at 11:17 UTC

    Those nested brackets make me think of Text::Balanced. Maybe that would be more convenient.

    After Compline,
    Zaxo

      Or you could try Damian's Regexp::Common
      use Regexp::Common; use strict; my @examples = ( '${ThisOne}', 'do not match me %{}', '${This${One}}', '${This${One}Too}', '${This${One}and${also}this}', 'me either $}', 'dont match this line noise {} $ }{' ); foreach (@examples){ print "$_\n" if /(\$$RE{balanced}{-parens=>'{}'})/; } my $longstring = join "", @examples; print "\n", join ("\n", $longstring =~ /(\$$RE{balanced}{-parens=>'{}'})/g); __OUTPUT__ ${ThisOne} ${This${One}} ${This${One}Too} ${This${One}and${also}this} ${ThisOne} ${This${One}} ${This${One}Too} ${This${One}and${also}this}

      flounder

Re: Regexp Question
by I0 (Priest) on Jun 11, 2002 at 16:18 UTC
    $_ = '${ThisOne} ${This${One}} ${This${One}Too} ${This${One}and${also}this}'; ($re=$_)=~s/((\$\{)|(\})|.)/${[')','']}[!$3]\Q$1\E${['(','']}[!$2]/g; print join"\n",eval{/$re/},"",""; warn $@ if $@ =~ /unmatched/; $re = join '|',map{quotemeta}eval{/$re/}; print join"\n",/\$\{($re)\}/g;
Re: Regexp Question
by talexb (Chancellor) on Jun 11, 2002 at 12:44 UTC
    I wonder if Parse::RecDescent would be suitable for this problem instead of writing a monster regexp.

    At least it would more easily handle the recursion that is suggested by your examples.

    --t. alex

    "Nyahhh (munch, munch) What's up, Doc?" --Bugs Bunny

Re: Regexp Question
by Anonymous Monk on Jun 11, 2002 at 11:27 UTC
    $test="\$\{This\$\{One\}and\$\{also\}this\}"; $test=~/\$\{(([^\{\$\}]*(?:\$\{[^\$\{\}]+\}[^\$\{\}]*)*))\}/gxs; print "test:$1";
    Try this.I haven't made any changes in your regexp i just added two brackets to get the required values($1)