On Jan 1, 4:20 pm, [EMAIL PROTECTED] wrote:
> Hi there,
>
> I'm quite new to perl, and now having problem with using  parentheses
> in translation strings. For example, I want to replace the 4 digit
> year code 'yyyy' with '(\d\d\d\d)', so it can be used for grouping in
> string matching later. I expected the code would be like:
>
> $org_str =~ tr/yyyy/\(\d\d\d\d\)/;
>
> Then the result is '(((('. It seems the parens were not escaped
> properly, shouldn't the backslash escape the round brackets? If I
> remove the backslashes before brackets, the result is the same. If I
> use double backslashes, that is, \\(\d\d\d\d\\), the result is '\\\\'.
> Can someone inform the right way to do this? Many thanks in
> advance! :-)

This has nothing to do with parentheses nor backslashes.  It has to do
with you confusing the tr/// operator with the s/// operator.

tr/// takes a list of characters on the left, and replaces them with
the corresponding character from the list on the right.    It does not
replace strings.  You are saying to replace the character 'y' with the
character '(', since 'y' is the first character on the left and '(' is
the first character on the right.   The fact that 'y' is also the 2nd,
3rd, and 4th character on the left is completely ignored.  tr///
already has 'y's replacement.

You want to use the s/// operator, which will replace the string
'yyyy' with the string '(\d\d\d\d)', so long as you remember to escape
the backslashes:

s/yyyy/(\\d\\d\\d\\d)/;
or:
s/y{4}/(\\d\\d\\d\\d)/;

for more information:
perldoc perlop
perldoc perlre

Paul Lalli


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to