英文:
Regex to capture a repeated group of two words
问题
我正在尝试构建一个可以捕获两个连续单词组的表达式。例如:
Glass Magnifying Glass Magnifying.svg
应该变成:
Glass Magnifying.svg
我有一个很好的用于捕获单词重复的表达式:
\b(\w+)\s+\1\b
但我不知道如何修改它以捕获双词重复。
我没有包含语言标签,因为我在一个叫做Renamer的程序中使用这些表达式。
任何帮助将不胜感激!
编辑:
以下是更多示例:
之前:
Glass Magnifying Glass Magnifying.svg
Question Mark Question Mark.svg
Quote Left Quote Left Alt.svg
Read More Read More.svg
Stroke Comment Stroke Comment Alt2 Alt.svg
Stroke Heart Stroke Heart.svg
使用正则表达式后:
Glass Magnifying.svg
Question Mark.svg
Quote Left Alt.svg
Read More.svg
Stroke Comment Alt2 Alt.svg
Stroke Heart.svg
英文:
I am trying to form an expression that will capture a repeated group of two words. For example:
Glass Magnifying Glass Magnifying.svg
Should become:
Glass Magnifying.svg
I have an expression that works great that captures single word duplicates:
\b(\w+)\s+\1\b
But I don't have any idea how to modify this to capture double word duplicates.
I didn't include a language tag because I'm using the expressions in a program called Renamer.
Any help would be greatly appreciated!
Edit:
More examples below:
Before:
Glass Magnifying Glass Magnifying.svg
Question Mark Question Mark.svg
Quote Left Quote Left Alt.svg
Read More Read More.svg
Stroke Comment Stroke Comment Alt2 Alt.svg
Stroke Heart Stroke Heart.svg
After regex:
Glass Magnifying.svg
Question Mark.svg
Quote Left Alt.svg
Read More.svg
Stroke Comment Alt2 Alt.svg
Stroke Heart.svg
答案1
得分: 1
\b([\w ]+)\s+\1\b
英文:
You can simply include a space in your first match:
\b([\w ]+)\s+\b
答案2
得分: 1
你可以使用以下内容:
(\w+\s\w+)\s
或者,如果单词中有连字符:
([\w-]+\s[\w-]+)\s
英文:
You can use the following.
(\w+\s\w+)\s
Or, if the words have hyphens.
([\w-]+\s[\w-]+)\s
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论