Using s/// in an expression

Posted by mikeY on Stack Overflow See other posts from Stack Overflow or by mikeY
Published on 2010-04-19T01:47:58Z Indexed on 2010/04/19 1:53 UTC
Read the original article Hit count: 247

Filed under:
|
|
|
|

I got a headache looking for this: How do you use s/// in an expression as opposed to an assignment. To clarify what I mean, I'm looking for a perl equivalent of python's re.sub(...) when used in the following context:

newstring = re.sub('ab', 'cd', oldstring)

The only way I know how to do this in perl so far is:

$oldstring =~ s/ab/cd/;
$newstring = $oldstring;

Note the extra assignment.

© Stack Overflow or respective owner

Related posts about perl

Related posts about python