in reply to Regex help

Another way to do this would be with split.

my( $start, $middle, $end ) = split /((?:AB)+)/,$string ;

Much of a muchness in this case, but it does show the little used technique of using capturing brackets with split to retain the bits that would otherwise be discard, which is sometimes useful.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.

Replies are listed 'Best First'.
Re: Re: Regex help
by bart (Canon) on Aug 24, 2003 at 16:25 UTC
    No, you really should have done this:
    my($start, $middle, $end) = split /((?:AB)+)/, $string, 2;
    Try something that contains this pattern twice, and you'll immediately see the difference, as in
    $string = "xAByABz";
    Yours would have put just "y" into $end, mine takes the entire rest of the string, "yABz".