Reputation: 320
I've made a number of tries in the past to get a grip of regexes, I have solved the problems, but still no clear understanding.
For now, I've solved a problem too, but solution is so ugly. So, i have a number of quoted strings delimited by spaces:
"string" "string two" "lots of strings"
I need to upper case every word. So I Find&Replace (.)
for \u\1
and the second iteration "(.)
for "\u\1
The questions are:
quote
and after space
)of
etc)and optional question
I've searched the internet and had found a parts of answers, but it just parts, I don't know how to combine them. And other examples, also, I can't translate them: seems like they consist of one string that does substitution, but here I have two fields for find and replace. Pardon my ignorance. I am ashamed of it, but curiosity gets the upper hand.
Upvotes: 2
Views: 4922
Reputation: 91518
Here is how I'do it:
Find what: \b(?!of|the)(\w)(\w+)
Replace with: \u$1\E$2
"string" "string two" "lots of the strings"
becomes:
"String" "String Two" "Lots of the Strings"
You can add every words you want in the negative lookahead: (?!of|the|in|out)
Upvotes: 4
Reputation: 3039
I did not try this in Notepad++, (see below) but my Textpad editor (which is fairly similar to Notepad++) handles replacing \<(.)
to \u$1
correctly. (Notice the different character sequence to identify word boundaries, \<
for Textpad and \b
for Perl.) After filling in rearch and replacement just check the "Regular Expression" checkbox, hit "Replace all" and thats it.
For the other goals, the regex libraries used in editors may not be powerful enough, because they support only a subset of the regex syntax. For lowercasing the rest of the word if its not all-uppercase, it would be easiest to use assertions, which are not supported by Textpad.
If you want to find out more about regular expressions, I would recommend to use one of the online regular expression testers, because they do not have the limitations of the editor's libraries, and have useful features like highlighting for matches and regular expression syntax.
Excluding word lists an stuff like that usually look rather ugly in regular expressions. Be sure to understand the idea behind regular expressions. They are engines which are very efficient at decision-making, but not at looking-up anything. But of course, a small word-list like (of|if|and|or|not|ping|pong) does not hurt anyone.
EDIT: ([" ])(.)
and $1\u$2
work as well and are more close to your specification and do not convert I've to I'Ve. And I found out that Textpad regexes support look-ahead and look-behind assertions as well, as of Textpad 7.
EDIT:
they consist of one string that does substitution
Be aware of the Perl syntax for regular expressions, which looks like
$<variable> =~ s/<search>/<replacement>/<modifiers>
EDIT: Verfified that at least ([" ])(.)
and $1\u$2
work in Notepad++ as well.
Upvotes: 1
Reputation: 1363
This is possible in Perl:
#!/usr/bin/perl
$test_data = '"string" "string two" "lots of strings"';
$test_data =~ s/\b(.)/\u$1/go;
print $test_data . "\n";
but I'm not sure if it's possible in Notepad++.
Upvotes: 2