On 13 April 2011 11:40, Shlomit Afgin <shlomit.af...@weizmann.ac.il> wrote:
> > > > > Hi > > > I need to write regular expression that will capitalize the first letter of > each word in the string. > Word should be string with length that is greater or equal to 3 letters > exclude the words 'and' and 'the'. > > > I tried: > $string = lc($string); > $string =~ s/\b(\w{3,}[(^the|^and)])\b/ucfirst($1)/ge; > but it not working so well. > > You are matching two words every time here , I dont think you can do it this way This works while(<DATA>){ chomp; $string = lc($_); $string =~ s/\b(\w{3,})/subword($1)/ge; print "$string\n"; } sub subword { return $_[0] if($_[0] =~/^(the|and)$/); return ucfirst($_[0]); } __DATA__ Once in a garden The quick brown fox jumped over the lazy dog and did not wake him up