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?
Haven't tested this but something like this:
that did it!
first i had to lowercase everything with
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.
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...
I agree the idea to make all lowercase first is ideal!
but, what is the $data =~ tr/A-Z/a-z/; doing?
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.
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?
Yes, I am reading tons of examples and getting crazy!
i'm no regular expressions expert, but i can offer a little explanantion.
tr/target set/replacement set/;
\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.
|All times are GMT +1. The time now is 07:44 PM.|
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.