Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Probably a dumb s/// question. (http://www.velocityreviews.com/forums/t891378-probably-a-dumb-s-question.html)

Mark Healey 03-16-2005 04:14 PM

Probably a dumb s/// question.
 
I'm trying to craft a search that capitalizes letters depending on their
context, specifically after a space or the beginning of a string.

For example I'd like to turn

the quick brown fox jumped over the lazy dogs.

to

The Quick Brown Fox Jumped Over the Lazy Dogs.

Is this doable on a single line?


--
Mark Healey
marknews(at)healeyonline(dot)com


Paul Lalli 03-16-2005 04:27 PM

Re: Probably a dumb s/// question.
 
"Mark Healey" <die@spammer.die> wrote in message
news:pan.2005.03.16.16.17.19.236667@spammer.die...
> I'm trying to craft a search that capitalizes letters depending on

their
> context, specifically after a space or the beginning of a string.
>
> For example I'd like to turn
>
> the quick brown fox jumped over the lazy dogs.
>
> to
>
> The Quick Brown Fox Jumped Over the Lazy Dogs.
>
> Is this doable on a single line?


What have you tried so far?

Have you read the posting guidelines for this group, posted twice a
week?

Because I'm feeling generous (and bored) anyway:

s/(^|\s)([a-z])/$1\u$2/g;


for more information on ^, |, (), $1 & $2:
perldoc perlre
perldoc perlretut
perldoc perlreref

for more information on \u:
perldoc -f ucfirst

Paul Lalli


Glenn Jackman 03-16-2005 04:51 PM

Re: Probably a dumb s/// question.
 
At 2005-03-16 11:14AM, Mark Healey <die@spammer.die> wrote:
> For example I'd like to turn
> the quick brown fox jumped over the lazy dogs.
> to
> The Quick Brown Fox Jumped Over the Lazy Dogs.
>
> Is this doable on a single line?


my $string = 'the quick brown fox jumped over the lazy dogs.';
my $String = join ' ', map {ucfirst lc} split ' ', $string;

That forces your string to lower case first then capitalizes the first
letter. It won't preserve whitespace though.

--
Glenn Jackman
NCF Sysadmin
glennj@ncf.ca

Ted Zlatanov 03-16-2005 06:19 PM

Re: Probably a dumb s/// question.
 
On Wed, 16 Mar 2005, die@spammer.die wrote:

> I'm trying to craft a search that capitalizes letters depending on their
> context, specifically after a space or the beginning of a string.
>
> For example I'd like to turn
>
> the quick brown fox jumped over the lazy dogs.
>
> to
>
> The Quick Brown Fox Jumped Over the Lazy Dogs.
>
> Is this doable on a single line?


Generally no, because of strange combinations like "Dog+Cat" or
"yes/no". There is a tool that will do it, but the internals are much
more than a single line :)

http://search.cpan.org/~doom/Text-Ca.../Capitalize.pm

Always check CPAN first.

Ted

Gunnar Hjalmarsson 03-16-2005 06:22 PM

Re: Probably a dumb s/// question.
 
Mark Healey wrote:
> I'm trying to craft a search that capitalizes letters depending on their
> context, specifically after a space or the beginning of a string.
>
> For example I'd like to turn
>
> the quick brown fox jumped over the lazy dogs.
>
> to
>
> The Quick Brown Fox Jumped Over the Lazy Dogs.

----------------------------------^

What determines that "the" is *not* converted to "The"?

> Is this doable on a single line?


If it is, I suppose it would be a *very* long line. :)

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Ted Zlatanov 03-16-2005 06:22 PM

Re: Probably a dumb s/// question.
 
On 16 Mar 2005, xx087@freenet.carleton.ca wrote:

> my $string = 'the quick brown fox jumped over the lazy dogs.';
> my $String = join ' ', map {ucfirst lc} split ' ', $string;
>
> That forces your string to lower case first then capitalizes the first
> letter. It won't preserve whitespace though.


It's probably better to do something like this:

perl -p -e's/(\w+)/ucfirst($1)/eg'

Text::Capitalize is even better, but the above will be closer to what
the OP wanted I think.

HTH
Ted

Ted Zlatanov 03-16-2005 06:30 PM

Re: Probably a dumb s/// question.
 
On Wed, 16 Mar 2005, mritty@gmail.com wrote:

> s/(^|\s)([a-z])/$1\u$2/g;


I always find it better to work with Perl built-ins such as ucfirst
and \w, which respect locale and know about Unicode uppercasing rules.
Any time I see a range like [a-z] or [A-Za-z] I try to reduce it to at
least a POSIX class like [:alpha:] unless I must only accept [a-z].

Ted

John W. Krahn 03-16-2005 07:50 PM

Re: Probably a dumb s/// question.
 
Glenn Jackman wrote:
> At 2005-03-16 11:14AM, Mark Healey <die@spammer.die> wrote:
>
>> For example I'd like to turn
>> the quick brown fox jumped over the lazy dogs.
>> to
>> The Quick Brown Fox Jumped Over the Lazy Dogs.
>>
>> Is this doable on a single line?

>
>
> my $string = 'the quick brown fox jumped over the lazy dogs.';
> my $String = join ' ', map {ucfirst lc} split ' ', $string;
>
> That forces your string to lower case first then capitalizes the first
> letter. It won't preserve whitespace though.


If you want to preserve whitespace:

my $String = join '', map {ucfirst lc} split /(\s+)/, $string;


John
--
use Perl;
program
fulfillment

Brian McCauley 03-16-2005 07:50 PM

Re: Probably a dumb s/// question.
 


Ted Zlatanov wrote:

> On Wed, 16 Mar 2005, mritty@gmail.com wrote:
>
>
>>s/(^|\s)([a-z])/$1\u$2/g;

>
>
> I always find it better to work with Perl built-ins such as ucfirst


Err... is \u not the same thing as ucfirst?


Tad McClellan 03-16-2005 11:52 PM

Re: Probably a dumb s/// question.
 
Ted Zlatanov <tzz@lifelogs.com> wrote:
> On Wed, 16 Mar 2005, mritty@gmail.com wrote:
>
>> s/(^|\s)([a-z])/$1\u$2/g;

>
> I always find it better to work with Perl built-ins such as ucfirst
> and \w, which respect locale and know about Unicode uppercasing rules.


\u _is_ ucfirst(), it respects locales too.


> Any time I see a range like [a-z] or [A-Za-z]



Ahh, now that is a different thing.


> I try to reduce it to at
> least a POSIX class like [:alpha:] unless I must only accept [a-z].



For this application, we don't need to ensure that it is a letter at all:

s/(^|\s)(.)/$1\u$2/g;

:-)


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas


All times are GMT. The time now is 08:40 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.