View Single Post
  #4 (permalink)  
Old March 7th, 2007, 07:32 AM
ciderpunx ciderpunx is offline
Friend of Wrox
Points: 1,515, Level: 15
Points: 1,515, Level: 15 Points: 1,515, Level: 15 Points: 1,515, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Ah ok

Code:
$str =~ s/(M)en/$1an/;
Or, more simply:

Code:
$str =~ s/Men/Man/;
If you want to change every instance then you can do

Code:
$str =~s/(M)en/$1an/g;
If you only want to change the word Men, and not Mendicant, for example, or HeMen then ad \b (word boundaries). i.e.

Code:
$str=~s/(\b)(M)en(\b)/$1$2an$3/;
This last example gives me

Code:
$ echo The men in the room looked at the Men in the street |  perl -pe 's/(\b)(M)en(\b)/$1$2an$3/g'
The men in the room looked at the Man in the street
$
You may want to downcase the M from Men, so you could do:

Code:
$ echo The men in the room looked at the Men in the street |  perl -pe 's/(\b)(M)en(\b)/$1\l$2an$3/g'
The men in the room looked at the man in the street
$
HTH
Charlie

--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
Reply With Quote