正则表达式,用于查找该表达式第一次出现后所有进一步出现的表达式

问题描述 投票:0回答:1

我有一行文本想要删除重复项(可能不必直接位于另一行文本之后)。

例如,我想删除这些行中的“First”:

First  Second  Third  First  Fourth  First  Fifth  ->   First  Second  Third  Fourth  Fifth
First  Second  Third  First  First  Fourth  Fifth  ->   First  Second  Third  Fourth  Fifth

我怎样才能实现这个目标?

regex duplicates cpu-word
1个回答
0
投票

如果它们正确地采用升序形式(如果没有重复项),那么也许您可以“逃脱”的正则表达式如下:

(First)?\s+(Second)?\s+(Third)?\s+(Fourth)\s+(Fifth)

这将捕获以下内容:

First Second Third Fourth Fifth
First Second Third
Second Third
First Fifth
© www.soinside.com 2019 - 2024. All rights reserved.