I agree completely with you, clean code is the best documentation.

But in your snippet I have to say: The use of $& anywhere in a program
imposes a considerable performance penalty on all regular expression
matches.

it would be better to avoid default/magic variables. I would consider
this snippet more clear:

#!/usr/local/bin/perl
use v5.10;
use strict;
use warnings;

my $text = "jack and jill went up the hill to fetch a pail of water";
$text =~ s~(\w{3,})~\u$1~g;
$text =~ s~\b(and)\b~\l$1~ig;
$text =~ s~\b(the)\b~\l$1~ig;
say $text;

Best Regards
Marcos Rebelo

On Thu, Apr 14, 2011 at 22:37, John Delacour <johndelac...@gmail.com> wrote:
> At 13:39 +0300 12/04/2011, Shlomit Afgin wrote:
>
>> I need to write regular expression that will capitalize the first letter
>> of each word in the string. Word should be word that her length 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.
>
> Why not just keep it simple so that you don't need to spend ten minutes
> working out what the regex means next time you look at the code.
>
>
> #!/usr/local/bin/perl
> use strict;
> $_ = "jack and jill went up the hill to fetch a pail of water";
> s~\w{3,}~\u$&~g;
> s~\band\b~\l$&~ig;
> s~\bthe\b~\l$&~ig;
> print;
>
>
>
> JD
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>



-- 
Marcos Rebelo
http://www.oleber.com/
Milan Perl Mongers leader https://sites.google.com/site/milanperlmongers/
Webmaster of http://perl5notebook.oleber.com

-- 
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