in reply to Re: Re: Substitute _last_ occurence in string?
in thread Substitute _last_ occurence in string?

This assumes that there is more than one 'two'. If there's only one instance, then the first instance is the last instance. I'd change this to:

$string =~ s/(.*)\s*two/$1/; That way, you'll catch it if it's the first characters in the string.

Update: Ok. Given the suggestions, How about:

$string =~ s/(.*)(^|\s+)two/$1/;

Replies are listed 'Best First'.
Re4: Substitute _last_ occurence in string?
by Hofmator (Curate) on Jul 25, 2001 at 19:22 UTC

    This assumes that there is more than one 'two'
    No, it doesn't. The only problem comes from the handling of surrounding spaces, but that goes for most solutions here. Your solution eats up all whitespace before the 'two' - and kills words like 'onetwo'. These things might or might not be OK, depending on what you want to do with the string afterwards ... but we don't know.

    So there is mtowtdi - surprise :) - but without more information, we can't tell which works best.

    -- Hofmator

Re: Re: Re: Re: Substitute _last_ occurence in string?
by carlitos (Acolyte) on Jul 26, 2001 at 14:39 UTC
    But that would match and replace the two in "twenty-two" as well, and probably this is not the desired behaviour.