Hi,

why not use tr?

$str =~ tr/io/10/;


Brs

 
想念你的每一天


----- 原邮件 -----
发件人: "Hamann, T.D." <thomas.ham...@naturalis.nl>
收件人: "beginners@perl.org" <beginners@perl.org>
抄送: 
发送日期: 2013年1月2日, 星期三, 10:34 下午
主题: Substituting letters for numbers

Hello,

Given a string:

i994

where I want to replace the 'i' by a '1' the following regex succesfully 
replaces the letter by a number:

s/(i)(\d\d\d)/1$2/;

However, given a string:

i99o

where I want to replace the 'i' by a '1' and the 'o' by a '0' (zero), the 
following regex fails:

s/(i)(\d\d)(o)/1$20/;

for the obvious reason that perl looks for a pattern match at bracket set #20, 
which doesn't exist.

I can fix this by inserting a space in front of the zero, like this:

s/(i)(\d\d)(o)/1$2 0/;

and then using a second regular expression to remove the space, but that 
somehow seems silly. Surely there is a quicker way to do this?

Regards,
Thomas Hamann


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to