keune
keune

Reputation: 5795

Get only the first of multiple occurrences

I have this regex:

(\w+)(?=.*\1)

This successfully matches the multiple occurrence of words. There is a problem, though. Consider this case:

dog cat dog cat cat

The regex matches cat twice, since there are two occurrences of cat that are followed by cat. But I only want one match for every word that occurs multiple times.

How can I do that?

Upvotes: 1

Views: 68

Answers (1)

vks
vks

Reputation: 67968

Yes it can be done by regex.

Instead of matching use replace.Replace by empty string.See demo.

https://regex101.com/r/qT5pO4/2

Upvotes: 2

Related Questions