Grep and Caps

544 views
Skip to first unread message

Ragnar

unread,
Apr 15, 2012, 10:21:40 PM4/15/12
to TextWrangler Talk
Is it possible to use grep to change all words that have at least
three characters, and are all in upper case, to only having the first
letter be upper case? For example, changing

THE US STAR-SPANGLED BANNER

O! say can you see by the dawn's early light,

to....

The US Star-Spangled Banner

O! say can you see by the dawn's early light,



It would be nice if the Change Case had this option.

Thomas Humiston

unread,
Apr 16, 2012, 12:46:30 AM4/16/12
to textwr...@googlegroups.com
Check 'case sensitive' and 'Grep' in the Find dialog. Then find:

([A-Z])([A-Z]{2,})

and replace with:

\1\L\2

([A-Z]) matches a letter in this range and remembers it as "\1" (the first set of parentheses). That's the first letter of a word, but we need at least 2 more, so...

([A-Z]{2,}) matches at least two occurrences of a letter in this range (no upper limit is specified), and remembers them as "\2" (because it's the second set of parentheses in the pattern).

In the replacement pattern, \1 tells TW to use that text just the way it is.

\L\2 tells TW to make whatever's in \2 lowercase. (If we used a lowercase L, it would make only the first character of \2 lowercase, like this: BaNNER.)


- TH
Kalamazoo, Mich.

Christopher Bort

unread,
Oct 26, 2012, 4:42:43 PM10/26/12
to textwr...@googlegroups.com
On 10/26/12 at 4:24 AM, mdr...@gmail.com (Martin Andrews) wrote:

>Hi,
>I'm looking to replace all UPPERCASE text with lowercase but
>only within the lower and greater than tags < > Please can you
>advise how to achieve this?

Page 150 in the TW User Manual discusses case transformations in
replacement patterns. For something like what you describe, you
might use a search pattern like:

<(.*?)>

and replace with something like

<\L\1>

>On Monday, 16 April 2012 03:21:40 UTC+1, Ragnar wrote:
>>
>>Is it possible to use grep to change all words that have at
>>least three characters, and are all in upper case, to only
>>having the first letter be upper case? For example, changing
>>THE US STAR-SPANGLED BANNER
>>O! say can you see by the dawn's early light,
>>to....
>>The US Star-Spangled Banner
>>O! say can you see by the dawn's early light,
>>
>>
>>It would be nice if the Change Case had this option.
>
--
Christopher Bort
<top...@thehundredacre.net>

Reply all
Reply to author
Forward
0 new messages