Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > Transforming case with regex

Transforming case with regex
Thread Tools
Forum Regular
Join Date: Nov 2001
Location: Toronto Canada
Status: Offline
Reply With Quote
Apr 23, 2004, 05:11 PM
 
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.
     
qyn
Dedicated MacNNer
Join Date: Dec 2000
Location: sj ca
Status: Offline
Reply With Quote
Apr 23, 2004, 10:19 PM
 
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
Reply With Quote
Apr 24, 2004, 09:48 PM
 
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
Reply With Quote
Apr 26, 2004, 11:29 AM
 
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
Reply With Quote
Apr 26, 2004, 01:27 PM
 
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
Reply With Quote
Apr 26, 2004, 01:32 PM
 
Oops, looks like nfuria was posting as I was talking. That looks like it just might work.
     
   
Thread Tools
Forum Links
Forum 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 On
Top
Privacy Policy
All times are GMT -5. The time now is 09:32 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2