in reply to Matching a truncated word

how about?
$f =~ /f(o(o(b(a(r)?)?)?)?)?$/

Replies are listed 'Best First'.
Re: Re: Matching a truncated word
by scain (Curate) on Jul 31, 2001 at 21:12 UTC
    Wow, is that ugly. I frequently have a difficult time with nested parens in regexes, so I wrote a quick test script to use this regex to figure out where $1, $2 etc would go:
    #!/usr/bin/perl -w $string="foobar"; if ($string =~ /(f(o(o(b(a(r)?)?)?)?)?)$/) { print "$1\n"; print "$2\n"; print "$3\n"; }
    There is nothing really earth-shattering in the results:
    foobar oobar obar
    So essentially, this will do very much what I suppose John wants to do, as the most complete match goes to $1.

    Very nice, Sifmole, but I bet this tends to be a pretty slow regex.

    Scott

      I didn't test the speed, but I wouldn't be surprised to find out that it was slow. There are better ways to do this as are shown below; I however decided to be literal and present a regex to do what he wanted, since that was what he asked. :)