Regexp for ALL CAPS or Word Capitalization

Everything related to our flagship word processor.
Post Reply
Groucho
Posts: 496
Joined: 2007-03-03 09:55:06
Location: Europe

Regexp for ALL CAPS or Word Capitalization

Post by Groucho » 2007-11-06 01:16:17

When using TextWrangler for my oldest (early 70's) ASCII files I can capitalize the first or all the characters of a word by putting \u or \U right before \n, where it stands for the text found.
I can't actually get the same behavior in NWP.
Any suggestions?
Cheers, Henry.

User avatar
martin
Official Nisus Person
Posts: 4289
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Post by martin » 2007-11-06 15:02:36

There's no quick replace expression that converts the found text to uppercase (or title case). However, once you've done a Find All, you can simply use the menu Edit > Convert > To Uppercase.

Groucho
Posts: 496
Joined: 2007-03-03 09:55:06
Location: Europe

Post by Groucho » 2007-11-07 01:54:04

But what if the text you are to capitalize occurs in the middle of a search? You will hilite the whole found text and then… what?
I think there's a lack.
Henry

User avatar
martin
Official Nisus Person
Posts: 4289
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Post by martin » 2007-11-07 14:58:19

Depending on your search pattern, you might be able to use positive look ahead/behind to only select the bits of text you want to change case on.

Here's a simple example. Note that the notation for positive look ahead is "(?=expression)" and look behind is "(?<=expression)". So let's say you want to uppercase all text found between any angle brackets and your search pattern is normally "<.+?>". Instead we move the brackets to look ahead/behind groups "(?<=<).+?(?=>)". By doing this the brackets are not included in the matched text and will not be selected, leaving you free to do your case conversion.

Groucho
Posts: 496
Joined: 2007-03-03 09:55:06
Location: Europe

Post by Groucho » 2007-11-08 01:13:12

Thanks Martin
Anyway there are time you cannot tell exactly which part of text you want to change. Yes, most times I perform a Find All and then narrow the search within selected text. But it doesn't always work, as you seem to state. If, for example, my search yields three words, namely \1, \2 and \3, how do I tell the program to select and eventually capitalize only occurrence \2 and not the others?
Maybe, I am missing something. Should I go over the grep system again?
Groucho

User avatar
martin
Official Nisus Person
Posts: 4289
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Post by martin » 2007-11-08 13:49:11

Basically you want to group whatever part of the pattern is not the text you want to capitalize in the look ahead/behind. Let's represent your find expression like this: "(pre)(modify)(post)". Basically you want to transform it to be "(?<=pre)modify(?=post)". In this way only the text you want to change is not part of a look ahead/behind.

Looking at your example of modifying just the 2nd word, let's say your find pattern is:

(\w+)\s(\w+)\s(\w+)

And you want to capitalize just \2. You'd want to use this pattern instead:

(?<=\w+\s)\w+(?=\s\w+)

However, one rule is that look-behind (but not look-ahead) expressions must be a fixed number of characters (eg: the repetition operator is not allowed). In this particular case that's not a problem, as we only need to know that some word occurred beforehand and don't care about its extent. Thus for our purposes this pattern is equivalent:

(?<=\w\s)\w+(?=\s\w+)

I realize all this is way more complicated than a "\u" feature, but I'm just trying to give you some options. I'll certainly file an enhancement request that we add support for such replacement transformations.

Groucho
Posts: 496
Joined: 2007-03-03 09:55:06
Location: Europe

Post by Groucho » 2007-11-10 06:33:09

Thanks for your answer, Martin.
I admit I didn't go deep into the grep's folds. Grep is great. Sometimes it comes easy, others it takes some doing.
Cheers, Henry.
Groucho

Post Reply