Reman
Reman

Reputation: 8119

How do I highlight lines which does not have a duplicate string?

I'm trying to find a regex to indicate lines which does not have a duplicate string.

Input:

 hi hello  hello hi
hello hi hello hi
 text hello  halo hi
hello  hello hi
 hello  halo hi
hello  hello hi

I want to highlight all lines which do contain the word hello but do NOT have another hello on the same line.

Expected Output:
Only line 3 and 5 must be highlighted.

I tried with this regex:

\(hello\)\(.*hello.*\)\@!

but it doesn't do what I want to do.

How can I highlight a pattern on a line (or the entire line) when there is no double pattern on the same line?

Upvotes: 3

Views: 112

Answers (1)

odessos
odessos

Reputation: 243

Try this :

^\(\(.*hello.*\)\{2,}\)\@!.*hello.*$

explanation

  • \(.*hello.*\) : atom matching a sentence containing hello
  • \(\(foo\)\{2,}\) : atom matching at least twice the atom \(foo\)
  • \@! : unmatch if the last atom was matched
  • .*hello.* : match a sentence with hello
  • ^$ : to match the whole line

"Match a whole line not containing at least twice hello but containing hello"

Upvotes: 1

Related Questions