On Jul 15, Luca Veraldi said:

>I'm a Computer-Science student and I'm reading a book about Perl,
>Programming Perl III ed. published by O'Reilly. I find regular
>expressions very interesting and I tried to use a regex analyzed in the
>book, the one to check if a string is ()-balanced. I rewrote it to check
>balancing also for [] and {}.
>
>The regex is the following (I'm not so good writing regex, because it is
>the first time I study this argument, so I'm not so sure the following
>is correct or optimized...)

The regex you have written requires Perl 5.6.0 at least.  Is your RedHat's
Perl up-to-date?

>    qr{(?>([\(\[\{])(?:(?>[^\(\)\[\]\{\}]*)|(??{$re}))*(?(?{$1 eq "("})\)|(?(?{$1 eq 
>"["})\]|\})))}ox;

That looks pretty solid to me, except that it's very stingy in what it
allows to be inside balanced parts.

  my $balanced;
  $balanced = qr%
    (?:
      (?> [^][(){}]+ )  # non [] () {} chars
      |
      \[ (??{ $balanced }) \]  # [ ... ]
      |
      \( (??{ $balanced }) \)  # ( ... )
      |
      \{ (??{ $balanced }) \}  # { ... }
    )*
  %x;

  if ($text =~ /^$balanced$/) {
    # it's balanced
  }

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
I am Marillion, the wielder of Ringril, known as Hesinaur, the Winter-Sun.
Are you a Monk?  http://www.perlmonks.com/     http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc.     http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter.         Brother #734
**      Manning Publications, Co, is publishing my Perl Regex book      **


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to