Skip to content Skip to sidebar Skip to footer

Matching The Same Word Twice With A Regular Expression, Not Caring What The Word Is

Take the following input: foo.foo aefhiuafhiauefheiauh bar.bar jgoeiajgoieajogiae baz.foo ogiejaogijaeoigjea Say I want to match x.x where x is the same both sides of the dot. So

Solution 1:

Try this:

/(\w+)\.\1/g

Tested on http://regexpal.com/ and works.

Edit: added global modifier like TomTom correctly suggests.


Solution 2:

Should be with global!

/(\w+)\.\1/g;

Solution 3:

I'm providing an adaptation to the accepted answer for people using Visual Studio 2017 to find the same variable name repeated in the same line.

The search I used is:

(\w+) = \1 [&]

I was searching for VB string concatenations where I want to consider replacing them with either stringbuilder or the simplified &= operator so I wanted to replace:

MyString = MyString & something

with

MyString &= something

The expression works with find all so I was able to find all instances


Post a Comment for "Matching The Same Word Twice With A Regular Expression, Not Caring What The Word Is"