[EMAIL PROTECTED] wrote:
Hi there,
Hello,
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! :-)
The tr/// operator transliterates characters, it doesn't substitute strings. For that you need the s/// substitution operator:
$org_str =~ s/yyyy/(\\d\\d\\d\\d)/g; Or: $org_str =~ s'yyyy'(\d\d\d\d)'g; John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order. -- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/