> -----Original Message----- > From: Ramprasad A Padmanabhan [mailto:[EMAIL PROTECTED]] > Sent: 19 September 2002 11:18 > To: [EMAIL PROTECTED] > Subject: wordswap script > > > Hi All > > I am writing a script that will replace all instance of a > particular > word another in a file > > The only problem is that I want to match only whole words > eg If $str="the there thee that the"; > > I want to change to $str="abc there thee that abc"; > > > $srcstr = 'the' > $dest = 'abc' > > $str=~s/([^\w\_\-\.])\Q$srcstr\E([^\w\_\-\.])/$1$dest$2/g; > this doesnt seem to work >
The magic \b matches a 'word boundary' so try this: #!/usr/local/bin/perl -w use strict; my $str="the there thee that the"; my $old = 'the'; my $new = 'abc'; $str =~ s/\b$old\b/$new/g; Regards Jeff -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]