Reply to topic  [ 7 posts ] 
Regexp for ALL CAPS or Word Capitalization 
Author Message

Joined: 2007-03-03 09:55:06
Posts: 494
Location: Europe
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.


2007-11-06 01:16:17
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
Post 
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.


2007-11-06 15:02:36
Profile WWW

Joined: 2007-03-03 09:55:06
Posts: 494
Location: Europe
Post 
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


2007-11-07 01:54:04
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
Post 
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.


2007-11-07 14:58:19
Profile WWW

Joined: 2007-03-03 09:55:06
Posts: 494
Location: Europe
Post 
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


2007-11-08 01:13:12
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
Post 
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.


2007-11-08 13:49:11
Profile WWW

Joined: 2007-03-03 09:55:06
Posts: 494
Location: Europe
Post 
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


2007-11-10 06:33:09
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

Who is online

Users browsing this forum: No registered users and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software