in reply to RegEx Problem

Here's one I hadn't tried before -- /e nested within /e.
$phrase = "NoMatch cowsCanFly sheepAreVeryCool NoMatch"; $phrase =~ s{ \b ( [a-z]+ (?:[A-Z][a-z]+)+ ) \b }{ my $word = $1; $word =~ s/([A-Z])/"-" . lc($1)/eg; $word; }gex; print $phrase, "\n";

Replies are listed 'Best First'.
Re: Re: RegEx Problem
by samtregar (Abbot) on May 29, 2002 at 06:26 UTC
    If that works it's only because you got lucky. In general the regex engine is not reentrant. Seg-faults are the most common result of this kind of code...

    -sam

      Nope, the regex is fine as it isn't re-entrant. If there was regex *within* the regex then that would be re-entrant, but as far as perl is concerned you can do as you please in the replace part of a s///.

      So this code will break[1]

      my $str = "foo bar baz quux"; $str =~ s<([a-z]+) (?{ s| \1|\L$&|; })><\U$1>xg;
      But this is fine
      $str =~ s<([a-z]+ ?)><local $_ = $1; s/$1/\U$&/g; $_>eg;

      HTH

      _________
      broquaint

      [1] in theory, I'll be happy to change it to a proper re-entrant regex if someone will provide me with one

        D'oh. I stand corrected. Is this documented anywhere that you know of?

        -sam