 |
 |
Transforming case with regex
|
 |
|
 |
|
Forum Regular
Join Date: Nov 2001
Location: Toronto Canada
Status:
Offline
|
|
I've got some text in a very predictable formation - like "THIS AFTERNOON" - all-caps, one or two words, no punctuation. And I want to transform it to "This Afternoon". I found a way to transform it to all lowercase using vim syntax:
Code:
1,$s/\(\u\+\)/\L&/g
or to transform the first letter to uppercase:
Code:
1,$s/\(\l\+\)/\u&/g
But I can't think how to make the first letter uppercase and all following letters lowercase. A link to a good reference is an exceptable substitute for an answer. Also I do have access to perl regular expressions if I need them.
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Dec 2000
Location: sj ca
Status:
Offline
|
|
You really want the 'tr' command. I don't know of a good reference besides 'man tr'
But I would recommend using perl:
Code:
#!/usr/bin/perl
# Get the string in somehow
$in_string = "THIS AFTERNOON";
$out_string = join(" ", map{ ucfirst(lc($_)) } split(/\s/, $in_string));
print $out_string;
That's a very simplistic example (and probably handles the spaces wrong), but the key is running ucfirst(lc()) on each word.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: in front of the keyboard
Status:
Offline
|
|
Uh...is this a joke?
You have the answer right there in your own question
Apply those sequentially and your text is just how you want it.
|
|
signatures are a waste of bandwidth
especially ones with political tripe in them.
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jan 2004
Status:
Offline
|
|
This line will work in Perls:
Code:
s/(\w)(\w+)/\u$1\L$2\E/g;
Furia
Originally posted by calimehtar:
I've got some text in a very predictable formation - like "THIS AFTERNOON" - all-caps, one or two words, no punctuation. And I want to transform it to "This Afternoon". I found a way to transform it to all lowercase using vim syntax:
Code:
1,$s/\(\u\+\)/\L&/g
or to transform the first letter to uppercase:
Code:
1,$s/\(\l\+\)/\u&/g
But I can't think how to make the first letter uppercase and all following letters lowercase. A link to a good reference is an exceptable substitute for an answer. Also I do have access to perl regular expressions if I need them.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Nov 2001
Location: Toronto Canada
Status:
Offline
|
|
No, this isn't a joke. The problem, I should have explained, is I'm not working on the command line, I'm working in a very restrictive proprietary template-style web development environment. I don't know of a way to send two instructions at a single string except by nesting templates or something like and I was hoping for a single regex command that would do what I need. I do have access to Perl, but it's also in a very limited way. I'm going to have to ask around the office a bit, it's not obvious how I'd actually be able to apply any of your suggestions in this environment.
Thanks for the tips, regardless.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Nov 2001
Location: Toronto Canada
Status:
Offline
|
|
Oops, looks like nfuria was posting as I was talking. That looks like it just might work.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|