I-Blue
I-Blue

Reputation: 21

Determine if a string contains duplicate words

I have the following words in a text field (called Criticality):

Confidentiality;Confidentiality

I want to be able to determine if this field contains duplicate (or reoccurring words)

if Criticality == Confidentiality;Confidentiality then Yes (meaning it has words re-occurring.

if Criticality == Confidentiality;Availability then No (meaning it does not have any word re-occuring).

I have tried using regex match with the following syntax:

$(RegexMatch(Criticality,  /(\b\w+\b)(\s+\1)+/i))

but it does not work. Not sure if someone could guide me.

Upvotes: 2

Views: 189

Answers (2)

user557597
user557597

Reputation:

This is the regex to determine if your field contains duplicate words

(?<![^;])(\w+)(?![^;])[\S\s]*(?<![^;])\1(?![^;])

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

Upvotes: 0

Barmar
Barmar

Reputation: 780949

Your words are separated by ;, but the regexp requires them to be separated by \s+, which is whitespace.

If you want to allow any non-word characters to separate them, you can use \W+.

You should also put \b around the back-reference, so it only matches a whole word.

(\b\w+\b)(\W+\b\1\b)

DEMO

Upvotes: 1

Related Questions