Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding duplicate consecutive strings in vim using vim regex

So let's say I have this in my search file

Foo
Bez, Bez
Foobar
Foo

I want to search for Bez, Bez by using a regex.

This is what I have and I know it's not even remotely correct.

:%s/\([a-zA-Z]\),\([a-zA-Z])/\1,\1,\1/g

So basically what I want to do is make "Bez, Bez" into "Bez, Bez, Bez"

Really, I'm stumped on how to find 2 consecutive equivalent strings.

like image 230
Christopher Shifflett Avatar asked Apr 16 '26 08:04

Christopher Shifflett


2 Answers

what about:

%s/\(\w\+\), \1/\1, \1, \1/g

it captures the expression between the parenthesis even before ending the expression whole match, pretty neat huh?.

like image 116
Hassek Avatar answered Apr 17 '26 21:04

Hassek


You use capturing groups such as:

(\w+)\W+\1

but I don't recall the vim equivalent for such regex expression.

I tested using RegexPal and the input you gave

Test Equality in RegexPal

Edit

Found Back References in Vim

like image 28
gtgaxiola Avatar answered Apr 17 '26 20:04

gtgaxiola



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!