On 13/04/2011 07:10, Shlomit Afgin wrote:
> 
> 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.

Rules for title capitalisation are many and varied, but are always more
complex than just leaving 'and' and 'the' as lower case. A common rule,
and the one I prefer, is to capitalise all words except articles,
conjunctions, and prepositions.

Coding that would be a pain, so how about a module?

Rob



use strict;
use warnings;

use Text::Capitalize;

print capitalize_title('Regular expression to capitalize first letter of words 
in        sentence');

**OUTPUT**

Regular Expression to Capitalize First Letter of Words in Sentence

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to