[tpm] Greedy regex question

Jim Graham james.a.graham at gmail.com
Fri Feb 29 08:30:19 PST 2008


If you want to just strip off the last "(int)" or the last item in a  
"()", a simpler solution is probably

   $t = substr( $val, 0, rindex( $val, '(') );

  -- jim

On 29-Feb-08, at 11:07 AM, Madison Kelly wrote:

> Hi all,
>
>   Given the string:
>
> $val="0  (0x0)  (int)";
>
>   Why does:
>
> $val=~s/\(.*?\)//;
>
>   Properly, ungreedily, return "0    (int)" *but*, changing it to:
>
> $val=~s/\(.*?\)$//;
>
>   Wrongly, greedily, return "0  "? When I put the '.*?' in brackets  
> and
> print '$1' on the later it is indeed grabbing "0x0)  (int".
>
>   What is it about anchoring the regex to the end of the string cause
> it to be a greedy regex again? As you can probably gather, I am trying
> to get rid of /only/ the last '(foo)' in a given string.
>
> Thanks!
>
> Madi
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm



More information about the toronto-pm mailing list