Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Probably a dumb s/// question.

Reply
Thread Tools

Probably a dumb s/// question.

 
 
Mark Healey
Guest
Posts: n/a
 
      03-16-2005
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

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      03-16-2005
"Mark Healey" <> wrote in message
news...
> 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

 
Reply With Quote
 
 
 
 
Glenn Jackman
Guest
Posts: n/a
 
      03-16-2005
At 2005-03-16 11:14AM, Mark Healey <> 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

 
Reply With Quote
 
Ted Zlatanov
Guest
Posts: n/a
 
      03-16-2005
On Wed, 16 Mar 2005, 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
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      03-16-2005
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
 
Reply With Quote
 
Ted Zlatanov
Guest
Posts: n/a
 
      03-16-2005
On 16 Mar 2005, 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
 
Reply With Quote
 
Ted Zlatanov
Guest
Posts: n/a
 
      03-16-2005
On Wed, 16 Mar 2005, 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
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      03-16-2005
Glenn Jackman wrote:
> At 2005-03-16 11:14AM, Mark Healey <> 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
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      03-16-2005


Ted Zlatanov wrote:

> On Wed, 16 Mar 2005, 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?

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      03-16-2005
Ted Zlatanov <> wrote:
> On Wed, 16 Mar 2005, 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
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
probably a dumb question, but I cant figure it out.... Justin Java 2 10-09-2006 12:40 AM
Probably A really Dumb Enveloping Printing Question Rose Bush Computer Support 7 03-31-2005 09:38 PM
Probably a dumb s/// question. Mark Healey Perl 2 03-16-2005 04:51 PM
A Really Dumb (Probably) OE Question Pop Aye Computer Support 4 11-06-2004 05:27 AM
Dumb, dumb dumb Qestion David Napierkowski Digital Photography 6 10-31-2004 11:14 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57