[英]Notepad++ and regex - how to title case string between two particular strings?
I have hundreds of bib references in a file, and they have the following syntax:我在一个文件中有数百个 bib 引用,它们具有以下语法:
@article{tabata1999precise,
title={Precise synthesis of monosubstituted polyacetylenes using Rh complex catalysts.
Control of solid structure and $\pi$-conjugation length},
author={Tabata, Masayoshi and Sone, Takeyuchi and Sadahiro, Yoshikazu},
journal={Macromolecular chemistry and physics},
volume={200},
number={2},
pages={265--282},
year={1999},
publisher={Wiley Online Library}
}
I would like to title case (aka Proper Case) the journal name in Notepad++ using regular expression.我想使用正则表达式在 Notepad++ 中对期刊名称进行标题大小写(又名正确大小写)。 For example, from Macromolecular chemistry and physics
to Macromolecular Chemistry and Physics
.例如,从Macromolecular chemistry and physics
到Macromolecular Chemistry and Physics
。
I am able to find all instances using:我可以使用以下方法找到所有实例:
(?<=journal\=\{).*?(?=\})
but I am unable to change the case via Edit > Convert Case to.但我无法通过 Edit > Convert Case to 更改大小写。 Apparently it doesn't work on find all and I have to go one by one.显然它不适用于全部查找,我必须一一查找 go。
Next, I tried recording and running a macro but Notepad++ just hangs indefinitely when I try to run it (option to run until the end of the file).接下来,我尝试录制并运行一个宏,但是当我尝试运行它时,Notepad++ 只是无限期挂起(运行到文件末尾的选项)。
So my question is: does anyone know the replace regex syntax I could use to change the case?所以我的问题是:有人知道我可以用来改变大小写的替换正则表达式语法吗? Ideally, I would also like to use "|"理想情况下,我也想使用“|” exclusions for particular words such as " of ", " an ", " the ", etc. I tried to play with some of the examples provided here , but I was not able to integrate it into my look-aheads.排除特定单词,例如“of”、“an”、“the”等。我尝试使用此处提供的一些示例,但无法将其集成到我的前瞻中。
Thank you in advance, I'd appreciate any help.提前谢谢你,我会很感激任何帮助。
This works for any number of words:这适用于任意数量的单词:
(?:journal={|\G)\K(?:(\w{4,})|(\w+))(\h*)
找到什么: (?:journal={|\G)\K(?:(\w{4,})|(\w+))(\h*)
\u$1\E$2$3
替换为: \u$1\E$2$3
Explanation:解释:
(?: # non capture group
journal={ # literally
| # OR
\G # restart from last match position
) # end group
\K # forget all we have seen until this position
(?: # non capture group
(\w{4,}) # group 1, a word with 4 or more characters
| # OR
(\w+) # group 2, a word of any length
) # end group
(\h*) # group 3, 0 or more horizontal spaces
Replacement:替代品:
\u # uppercased the first letter of the following
$1 # content of group 1
\E # stop the uppercased
$2 # content of group 2
$3 # content of group 3
Screenshot (before):截图(之前):
Screenshot (after):截图(之后):
if the format is always in the form:如果格式始终为以下形式:
journal={Macromolecular chemistry and physics}, journal={高分子化学与物理},
ie journal followed by 3 words then use the following:即日志后跟3个单词,然后使用以下内容:
Find: journal={(\w+)\s*(\w+)\s*(\w+)\s*(\w+)
查找: journal={(\w+)\s*(\w+)\s*(\w+)\s*(\w+)
Replace with: journal={\u\1 \u\2 \l\3 \u\4
替换为: journal={\u\1 \u\2 \l\3 \u\4
You can modify that if you have more words to replace by adding more \u\x, where x is the position of the word.如果您有更多单词要替换,您可以通过添加更多 \u\x 来修改它,其中 x 是单词的 position。
Hope it helps to give you an idea to move forward for a better solution.希望它有助于给您一个想法,以寻求更好的解决方案。
\u translates the next letter to uppercase (used for all other words) \u 将下一个字母转换为大写(用于所有其他单词)
\l translates the next letter to lowercase (used for the word "and") \l 将下一个字母转换为小写(用于单词“and”)
\1 replaces the 1st captured () search group \1 替换第一个捕获的 () 搜索组
\2 replaces the 2nd captured () search group \2 替换第二个捕获的 () 搜索组
\3 replaces the 3rd captured () search group \3 替换第 3 个捕获的 () 搜索组
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.