Go Back   CodingForums.com > :: Server side development > Perl/ CGI

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-10-2002, 01:53 AM   PM User | #1
cedsn
New Coder

 
Join Date: Aug 2002
Location: san marcos TX
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
cedsn is an unknown quantity at this point
capitalizing variables for display

I'm sure this is something simple, but as I'm a bit new to regular expressions, it's giving me a headache.

what i want to do is take a character string of varying lengths of words and capitalize every word.. i have so far suceeded in taking of the first letter and lowercasing the rest as follows:

$brand =~ s/\w//;
$brand =~ tr/A-Z/a-z/;

and i assume there must be some way to put the first character in another variable and capitalize it, then put them back together.. but most likely i'm going about it all wrong. Can anyone tell me the easy way to do this?

thanks, jesse
cedsn is offline   Reply With Quote
Old 08-10-2002, 05:26 AM   PM User | #2
Mouldy_Goat
Regular Coder

 
Join Date: Jul 2002
Location: London, UK
Posts: 126
Thanks: 0
Thanked 0 Times in 0 Posts
Mouldy_Goat is an unknown quantity at this point
Haven't tested this but something like this:
Code:
$data =~ s/\b(\w)/\U$1/g;
Should do the trick I think...
Mouldy_Goat is offline   Reply With Quote
Old 08-10-2002, 06:22 AM   PM User | #3
cedsn
New Coder

 
Join Date: Aug 2002
Location: san marcos TX
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
cedsn is an unknown quantity at this point
that did it!

first i had to lowercase everything with
Code:
$data =~ tr/A-Z/a-z/;
then user your snippet there and worked like a charm.

thanks!
cedsn is offline   Reply With Quote
Old 08-10-2002, 07:25 PM   PM User | #4
Mouldy_Goat
Regular Coder

 
Join Date: Jul 2002
Location: London, UK
Posts: 126
Thanks: 0
Thanked 0 Times in 0 Posts
Mouldy_Goat is an unknown quantity at this point
By the way, there's a command lc() which returns the lowercase version of its argument.. so you could use:
$data = lc($data);
instead.. not sure that it makes too much difference.
Mouldy_Goat is offline   Reply With Quote
Old 10-28-2003, 02:43 AM   PM User | #5
mtm
Registered User

 
Join Date: Oct 2003
Location: US
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
mtm is an unknown quantity at this point
help?

This is an old thread, but the same problem..
I need any input of any length returned as first-letter-of-the-word-capitalized. As This Is...
<br>

I agree the idea to make all lowercase first is ideal!
but, what is the $data =~ tr/A-Z/a-z/; doing?

<br>
ans in the following...$data =~ s/\b(\w)/\U$1/g;
could you just treat me to an explanation as I'm a tad confused now.
<br>
Also, when the g or i or m is used, which way is it?
quotations? not? If there is a replace() function , then the g,m, or i goes out of the parens, or inside it?
<br>
Yes, I am reading tons of examples and getting crazy!

Many thanks...
mtm is offline   Reply With Quote
Old 11-03-2003, 06:46 PM   PM User | #6
cedsn
New Coder

 
Join Date: Aug 2002
Location: san marcos TX
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
cedsn is an unknown quantity at this point
i'm no regular expressions expert, but i can offer a little explanantion.

Quote:
but, what is the $data =~ tr/A-Z/a-z/; doing?
[tr]anslating any character in the A-Z set of capital letters to its corresponding lowercase character.. or in other words,

tr/target set/replacement set/;

Quote:
ans in the following...$data =~ s/\b(\w)/\U$1/g;
s = substitute

\b = border so it's looking for a word border
\w = word charachter (like a letter).. since it's in parentheses that means we can refer to this match later in the replacement area..

\U means to uppercase it
$1 refers to the match we found in parentheses, if there were multiple parenteses in our target pattern they would be referred to as $2, $3, etc

then the g; modifier means global, so it searches and replaces every match it can, the i modifier i think has to do with case, not sure about m, but i do know the modifier goes at the end. hope this helps, and there is plenty of info on regular expressions out there to look up though i find it pretty confusing.
cedsn is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 05:21 AM.


Advertisement
Log in to turn off these ads.