On 2/15/06, anand kumar <[EMAIL PROTECTED]> wrote: > > > "John W. Krahn" <[EMAIL PROTECTED]> wrote: anand kumar wrote: > > Hi all, > > Hello, > > > I have the following problem in the following regex replace. > > > > $line=~s!\b($name)\b!$1!g; > > > > here this regex finds the exact matching of the content in $name and does > > the needed but in some examples the variable $name may contain backslash > > characters like 'gene\l=s\' , in this type of cases the replace string does > > not work so i have removed '\b' on either side and used the following > > > > $line=~s!(\Q$name\E)!$1!g; > > > > This works fine but the problem is that the replacement is not done on the > > exact word but also on substrings which is unnecessary. > > > > if i use both \b\b and \Q\E then the code fails to replace. > > > > please send suggestions in this regard > > $line=~s!\b(\Q$name\E)\b!$1!g;
Try this: if $name is a single-quoted string: $name = quotemeta($name); $line =~ s|($name)|<au>$1|; If $name is a double-quoted string: $name = quotemeta(quotemeta($name)); $line =~ s|($name)|<au>$1|; It's preferable, though for $name to be single-quoted, because Perl will do some interpolation at the time the string is saved, and depending on your system, strange things can happen. For instance, the following are not all equal: $name = quotemeta("\aball"); # $name gets '\\x07ball' $name = '\aball'; # $name gets '\aball' $name = "\aball"; $name = quotemeta($name); # $name gets '\\x07ball' This is because the double-quoted string is interpolated before it is assigned to a variable or passed to a function and the metacharacter--in this case '\a', the escape sequence for the ASCII bell character--is already interpolated. HTH, -- jay -------------------------------------------------- This email and attachment(s): [ ] blogable; [ x ] ask first; [ ] private and confidential daggerquill [at] gmail [dot] com http://www.tuaw.com http://www.dpguru.com http://www.engatiki.org values of β will give rise to dom!