Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

vim

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?

like image 244
Reman Avatar asked Nov 23 '25 06:11

Reman


1 Answers

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"

like image 186
odessos Avatar answered Nov 25 '25 20:11

odessos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!