On Jan 2, 2008 10:48 AM, Chas. Owens <[EMAIL PROTECTED]> wrote: > On Jan 1, 2008 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! :-) > snip > > You want substitute (s///) not transliterate (tr///). The s/// > operator takes a regex in the first part and replaces what martches in > the target string it with the string in the second part. The tr/// > operator replaces the any character in the first argument with the > corresponding character from the second argument in the target string. > So you should be using > > $org_str =~ s/yyyy/(\\d\\d\\d\\d)/; >
I just thought of something. If it is your intent to use $org_str later in a regex you probably want the replacement string to be ([0-9]{4}) not (\\d\\d\\d\\d). The \d character class matches any numeric character. This includes characters like U+1811 (the Mongolian digit one). If you want only the characters 0 through 9, then you need to make that character class yourself: [0-9]. The {4} modifier say that there must be 4 of the last pattern (so it is that same thing as [0-9][0-9][0-9][0-9]). -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/