Christopher Shifflett
Christopher Shifflett

Reputation: 59

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.

Upvotes: 3

Views: 922

Answers (2)

gtgaxiola
gtgaxiola

Reputation: 9331

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

Upvotes: 1

Hassek
Hassek

Reputation: 8995

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?.

Upvotes: 4

Related Questions