Discussion:
Regular expression to capitalize first letter of words in sentence
Shlomit Afgin
2011-04-12 10:39:17 UTC
Permalink
Hi

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.

Any ideas?
Shlomit.
Shlomit Afgin
2011-04-13 06:10:02 UTC
Permalink
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.


Any ideas?
Shlomit.
John W. Krahn
2011-04-13 11:24:14 UTC
Permalink
Post by Shlomit Afgin
Hi
Hello,
Post by Shlomit Afgin
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;
[(^the|^and)] is a character class that matches *one* character, either
'(' OR '^' OR '|' OR ')' OR 'a' OR 'd' OR 'e' OR 'h' OR 'n' OR 't'.
Post by Shlomit Afgin
but it not working so well.
$ perl -le'
$_ = "I need to write regular expression that will capitalize the first
letter";
print;
$_ = lc;
print;
s/(\S+)/\u$1/g;
print;
'
I need to write regular expression that will capitalize the first letter
i need to write regular expression that will capitalize the first letter
I Need To Write Regular Expression That Will Capitalize The First Letter



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
http://learn.perl.org/
Ramprasad Prasad
2011-04-13 11:30:36 UTC
Permalink
Post by Shlomit Afgin
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'.
$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
Shlomi Fish
2011-04-13 11:46:20 UTC
Permalink
Hi Ramprasad,

thanks for your answer, but see below for my comments.
Post by Ramprasad Prasad
Post by Shlomit Afgin
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'.
$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 Prasad
This 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 Prasad
chomp;
$string = lc($_);
$string =~ s/\b(\w{3,})/subword($1)/ge;
Declare the $string variable using my.
Post by Ramprasad Prasad
print "$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/
C.DeRykus
2011-04-13 17:09:30 UTC
Permalink
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'.
$string = lc($string);
$string =~ s/\b(\w{3,}[(^the|^and)])\b/ucfirst($1)/ge;
A character class is the wrong strategy. For
more details about how character classes,
see: perldoc perlretut

Another possible solution:

s/\b(\w{3,})/ucfirst( lc $1)/ge;

--
Charles DeRykus
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
http://learn.perl.org/
Rob Dixon
2011-04-13 21:44:43 UTC
Permalink
Post by Shlomit Afgin
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.
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-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
http://learn.perl.org/
John Delacour
2011-04-14 20:37:08 UTC
Permalink
Post by Shlomit Afgin
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'.
$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-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
http://learn.perl.org/
marcos rebelo
2011-04-15 04:34:44 UTC
Permalink
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
Post by Shlomit Afgin
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'.
$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
--
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-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
http://learn.perl.org/
Loading...