正则表达式捕获重复的两个单词组

huangapple go评论55阅读模式
英文:

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

https://regex101.com/r/OUEczL/1

答案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

huangapple
  • 本文由 发表于 2023年6月12日 12:46:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453711.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定