简体   繁体   English

Notepad++ 和正则表达式 - 如何在两个特定字符串之间命名大小写字符串?

[英]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 physicsMacromolecular 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:这适用于任意数量的单词:

  • Ctrl + H Ctrl + H
  • Find what: (?:journal={|\G)\K(?:(\w{4,})|(\w+))(\h*)找到什么: (?:journal={|\G)\K(?:(\w{4,})|(\w+))(\h*)
  • Replace with: \u$1\E$2$3替换为: \u$1\E$2$3
  • CHECK Wrap around检查环绕
  • CHECK Regular expression CHECK正则表达式
  • Replace all全部替换

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.

相关问题 如何使用Regex删除Notepad ++中两个字符串之间的文本? - How to use Regex to remove text between two strings in Notepad++? 如何在记事本++上使用正则表达式仅保留特定字符串 - How to keep only a particular string using regex on notepad++ 如何使用正则表达式 Notepad++ 提取 2 个模式之间的所有字符串? - How to extract all the strings between 2 patterns using regex Notepad++? 如何在记事本中删除两个字符或字符串之间的空格 - How to remove a space between two characters or strings in notepad++ 如何使用RegEx在记事本中的两个单词之间添加“ _” - How to use RegEx to add “_” between two words with notepad++ 使用正则表达式记事本++将字符串的特定部分更改为标题或正确大小写 - Change specific part of a string to Title or Proper case using regex notepad++ 正则表达式替换Notepad ++中两个&#39;之间的空格 - Regex to replace space between two ' in Notepad++ notepad ++在这种情况下如何使用正则表达式? - notepad++ how to use regex in this case? 如何在Notepad ++中使用正则表达式捕获和替换字符串 - How to capture and replace strings with regex in Notepad++ Notepad ++如果其他字符串之间存在第三个字符串,则删除两个字符串之间的文本 - Notepad++ Removing text between two strings if third string is present between other strings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM