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.
Regexp for ALL CAPS or Word Capitalization
- martin
- Official Nisus Person
- Posts: 5230
- Joined: 2002-07-11 17:14:10
- Location: San Diego, CA
- Contact:
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.
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.
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?
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
- martin
- Official Nisus Person
- Posts: 5230
- Joined: 2002-07-11 17:14:10
- Location: San Diego, CA
- Contact:
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.
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.