如何使用正则表达式仅大写参考文献Title字段内的单词?
Let's fix your regex issue and get those title words properly capitalized!
Why Your Original Regex Didn't Work
Your regex (?<=Title)(\b.+?\b) only targets the first word after "Title" because:
- The non-greedy
.+?stops at the first word boundary (\b) right after "Title" - It doesn't restrict matches to the content inside the
{}brackets, so it could accidentally pick up unrelated text elsewhere in your file
Correct Approach: Target Title Content & Capitalize Each Word
We need to first isolate the text inside Title = { ... }, then capitalize the first letter of every word (including those after hyphens, like bi-elemental → Bi-Elemental).
1. For VS Code (Most Flexible)
Enable regex mode (click the .* button in the search/replace panel):
- Find:
(?<=Title = {)(.*?)(?=}) - Replace:
${1/(^|\s|-)([a-z])/$1\u$2/g}
This works by:
- Capturing all text inside the title brackets
- Running a nested regex on that captured text to find:
- The start of the string, a space, or a hyphen (
^|\s|-) - Followed by a lowercase letter (
[a-z])
- The start of the string, a space, or a hyphen (
- Replacing with the preceding character plus the uppercase version of the letter (
$1\u$2), applied globally (g)
2. For Sublime Text
Enable regex mode:
- Find:
(Title = {)(.*?)(}) - Replace:
$1${2/(\b|-)([a-z])/$1\u$2/g}$3
3. For Notepad++
Enable Perl-compatible regex (check the box in the replace dialog):
- Find:
(?<=Title = {)(\b[a-z]|[-][a-z]) - Replace:
\u$0
This directly matches every lowercase letter that starts a word (or comes after a hyphen) inside the title brackets, then uppercases it.
Example Result
Original line:
... Title = {Direct synthesis of antimicrobial coatings based on tailored bi-elemental nanoparticles}, journal={APL Mat.}, ...
After replacement:
... Title = {Direct Synthesis of Antimicrobial Coatings Based on Tailored Bi-Elemental Nanoparticles}, journal={APL Mat.}, ...
内容的提问来源于stack exchange,提问作者Giulio Benetti

