Notepad++ Capitalize Every First Letter of Every W

2020-02-17 04:45发布

问题:

Input: "notepad++ capitalize every first letter of every word"

Output: "Notepad++ Capitalize Every First Letter Of Every Word"

I have been attempting to capitalize the first letter of every word using ctr+F and regex.

So far I have been attempting to use find:\b(.) or \<(.) with replace:\u\1 but this results in all of my letters being capitalized.

I have made due with ^(.) & \u\1 followed by \s\b(.) & \u\1.

However, this seems silly to me as there are many posts talking about using the start of word boundaries. I am just having difficulty making them work. Thanks for your consideration!

回答1:

Background

According to Notepad++ specification (see Substitutions section), there are three operators that can be useful when turning substrings uppercase:

\u
Causes next character to output in uppercase
\U
Causes next characters to be output in uppercase, until a \E is found.
\E
Puts an end to forced case mode initiated by \L or \U.

Thus, you can either match a substring and turn its first character uppercase with \u to capitalize it, or match a character and use \U/\E.

Note that Unicode characters won't be turned uppercase, only ASCII letters are affected.


BOW (Beginning of Word) Bug in Notepad++

Note that currently (in Notepad++ v.6.8.8) the beginning of word does not work for some reason. A common solution that works with most engines (use it in Sublime Text and it will match) does not work:

\b(\w)

This regex matches all word characters irrespective of their position in the string.

I logged a bug Word boundary issue with a generic subpattern next to it #1404.


Solution #1 (for the current Notepad++ v.6.8.8)

The first solution can be using the \w+ and replace with \u$0 (no need using any capturing groups). Though this does not mean we only match the characters at the beginning of a word, the pattern will just match chunks of word characters ([a-zA-Z0-9_] + all Unicode letters/digits) and will turn the first character uppercase.


Solution #2 (for the current Notepad++ v.6.8.8)

The second solution can be implemented with special boundaries defined with lookbehinds:

(?:(?<=^)|(?<=[^\w]))\w

And replace with \U$0\E.

The regex (?:(?<=^)|(?<=[^\w]))\w matches an alphanumeric only at the beginning of a line ((?<=^)) or after a non-word character ((?<=[^\w])).

The replacement - \U$0\E - contains a \U flag that starts turning letters uppercase and \E is a flag that tells Notepad++ to stop converting case.


Edge case

In case you have hyphenated words, like well-known, and you only want the first part to be capitalized, you can use [\w-]+ with \u$0 replacement. It will also keep strings like -v or --help intact.



回答2:

A simpler regex that worked for me:

Find: (\w+)

Replace: \u$0



回答3:

There is a shortcut available in Notepad++ v7.3.2 to capitalize every first letter of every word.

ALT + U

Not sure about prior versions.



回答4:

Uppercase The First Letter Of Every Word: Use the shortcut: Alt + U

lowercase the first letter of every word: Use the shortcut: Clt + U

Shortcut working in version 7.6.3



回答5:

I have achieved something similar by recording a macro that uses the following replacement.

Find what:    ([a-z])+
Replace with: \u$0\E

Tick 'In selection'

This is the resulting macro that I extracted from C:\Users\%USERNAME%\AppData\Roaming\Notepad++\shortcuts.xml.

<Macro name="Title Case" Ctrl="no" Alt="no" Shift="no" Key="0">
    <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
    <Action type="3" message="1601" wParam="0" lParam="0" sParam="([A-Z])" />
    <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
    <Action type="3" message="1602" wParam="0" lParam="0" sParam="\L$0" />
    <Action type="3" message="1702" wParam="0" lParam="898" sParam="" />
    <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
    <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
    <Action type="3" message="1601" wParam="0" lParam="0" sParam="([a-z])+" />
    <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
    <Action type="3" message="1602" wParam="0" lParam="0" sParam="\u$0\E" />
    <Action type="3" message="1702" wParam="0" lParam="898" sParam="" />
    <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
</Macro>

Extra: you can add this to your right-click context menu (contextMenu.xml) using:

<Item MenuEntryName="Macro" MenuItemName="Title Case" />