alex
alex

Reputation: 95

regex is it possible to match a line that does not containg the same word repeated anywhere on the line?

how to select a line that contains one word but not repeated. let say i am looking for the string word1

text word1 text #matched

text word1 text word1 #not match , cause word1 is repeating.

word1 word1 word1 # not match , is repeating

word1 #matched

text text text text word1 # matched

can anyone giveme a hand how to develop a regex for matching those line??

Upvotes: 2

Views: 89

Answers (2)

Anirudha
Anirudha

Reputation: 32787

You can also try

 ^(?!.*(\bword1\b).*\b\1\b).*\bword1\b.*$

here word1 can be replaced with \w+ if you want to match any word..

use it with multiline option

Upvotes: 1

Bohemian
Bohemian

Reputation: 424983

Try this:

(\b\w+\b)(?!.*\1)

It uses a negative look ahead to a back reference to a captured word

Upvotes: 1

Related Questions