Hi Ramprasad,
thanks for your answer, but see below for my comments.
Post by Ramprasad PrasadPost by Shlomit AfginHi
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'.
$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
1. Always use "use strict;" and "use warnings;".
Post by Ramprasad PrasadThis works
while(<DATA>){
2. Input the line into an explicitly scoped variable:
while (my $line = <DATA>) {
3. Add a space before the "{".
Post by Ramprasad Prasadchomp;
$string = lc($_);
$string =~ s/\b(\w{3,})/subword($1)/ge;
Declare the $string variable using my.
Post by Ramprasad Prasadprint "$string\n";
}
sub subword {
return $_[0] if($_[0] =~/^(the|and)$/);
return ucfirst($_[0]);
}
1. Don't hardcode positional variables inside arrays $_[0]:
http://perl-begin.org/tutorials/bad-elements/#subroutine-arguments
2. You're using $_[0] more than once here, so it would better be a named
varialbe.
3. The regex match should be written as:
if ($word =~ m{\A(the|and)\z})
4. You're agai nmissing some surrounding spaces.
Regards,
Shlomi Fish
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
What does "Zionism" mean? - http://shlom.in/def-zionism
Stray XSLT code causes more deaths than road accidents.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
http://learn.perl.org/