Regular expression to capitalize first letter of words in

--=__Part94B8712A.0__=
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: quoted-printable





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 =3D lc($string);
$string =3D~ s/\b(\w{3,}[(^the|^and)])\b/ucfirst($1)/ge;
but it not working so well.


Any ideas?
Shlomit.

--=__Part94B8712A.0__=--
Shlomit Afgin [ Mi, 13 April 2011 08:10 ] [ ID #2058052 ]

Re: Regular expression to capitalize first letter of words in sentence

Shlomit Afgin wrote:
>
> Hi

Hello,

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

[(^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'.


> 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-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
jwkrahn [ Mi, 13 April 2011 13:24 ] [ ID #2058054 ]

Re: Regular expression to capitalize first letter of words in sentence

--000e0cd2e0843b48db04a0cb23c0
Content-Type: text/plain; charset=ISO-8859-1

On 13 April 2011 11:40, Shlomit Afgin <Shlomit.Afgin [at] 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

--000e0cd2e0843b48db04a0cb23c0--
Ramprasad Prasad [ Mi, 13 April 2011 13:30 ] [ ID #2058055 ]

Re: Regular expression to capitalize first letter of words in sentence

Hi Ramprasad,

thanks for your answer, but see below for my comments.

On Wednesday 13 Apr 2011 14:30:36 Ramprasad Prasad wrote:
> On 13 April 2011 11:40, Shlomit Afgin <Shlomit.Afgin [at] 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
>

1. Always use "use strict;" and "use warnings;".

> This works
> while(<DATA>){

2. Input the line into an explicitly scoped variable:

while (my $line = <DATA>) {

3. Add a space before the "{".

> chomp;

> $string = lc($_);
> $string =~ s/\b(\w{3,})/subword($1)/ge;

Declare the $string variable using my.

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

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-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Shlomi Fish [ Mi, 13 April 2011 13:46 ] [ ID #2058057 ]

Re: Regular expression to capitalize first letter of words in sentence

On Apr 12, 11:10=A0pm, Shlomit.Af... [at] weizmann.ac.il ("Shlomit Afgin")
wrote:
> Hi =A0
>
> I need to write regular expression that will capitalize the first letter =
of each word in the string. =A0
> Word should be string with length that is greater or equal to 3 letters =
=A0exclude the words 'and' and 'the'.
>
> I tried:
> $string =3D lc($string);
> $string =3D~ 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-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
derykus [ Mi, 13 April 2011 19:09 ] [ ID #2058059 ]

Re: Regular expression to capitalize first letter of words in sentence

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-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Rob Dixon [ Mi, 13 April 2011 23:44 ] [ ID #2058060 ]
Perl » gmane.comp.lang.perl.beginners » Regular expression to capitalize first letter of words in

Vorheriges Thema: Help using cgi
Nächstes Thema: Re: Oracle DBA's group mail ids'