in reply to Pattern Substitution..

You have the meanings right, but remember that whatever is matched on the left side of the s/// operator is completely replaced by the right hand side. Since perl's re engine is greedy, it will match as many o's in a row in the o+ case, and replace them all with a single e, or simply see no o's at the start of the string in the o* case, and put an e in front. You might try to have perl ignore greedy behavior with s/o*?/e/ and s/o+?/e/ and see what interesting results you get.

Note that if all you are trying to do is replace one character with another, it's much easier to use tr than s. For example:

$new_string = "good food"; $new_string =~ tr/od/ek/;
(update not like this as japhy points out...
$new_string = ( "good food" =~ tr/[od]/[ek]/; );
) will give you "geek feek", with 'o' converted to 'e', and 'd' converted to 'k'.


Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

Replies are listed 'Best First'.
Re: Re: Pattern Substitution..
by japhy (Canon) on May 30, 2001 at 18:31 UTC
    No, it won't. tr/// in scalar context returns the number of characters translated; and the brackets are unnecessary. Not to mention, you're trying to modify a constant string. <code> ($new_string = "good food") =~ tr/od/ek/;

    japhy -- Perl and Regex Hacker