Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching the same word twice with a regular expression, not caring what the word is

Tags:

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 I don't want to match x.y. So with the example input, I'd get foo.foo, bar.bar and not baz.foo

What I want to do is something like

(\w+)\.$1

But of course that doesn't work.

Is this possible in any sane way with a regex, or should I be matching x.y and handling the comparison of x and y in code?

For the sake of the question, assume I'm using the Javascript regex engine.

like image 438
PhonicUK Avatar asked Feb 13 '13 15:02

PhonicUK


People also ask

What will the regular expression match?

By default, regular expressions will match any part of a string. It's often useful to anchor the regular expression so that it matches from the start or end of the string: ^ matches the start of string. $ matches the end of the string.

What is regular expression used for?

Regular expressions are particularly useful for defining filters. Regular expressions contain a series of characters that define a pattern of text to be matched—to make a filter more specialized, or general. For example, the regular expression ^AL[.]*

Which pattern is used to match any non What character?

The expression \w will match any word character. Word characters include alphanumeric characters ( - , - and - ) and underscores (_). \W matches any non-word character.

What is the use of regular expression in Python?

A RegEx, or Regular Expression, is a sequence of characters that forms a search pattern. RegEx can be used to check if a string contains the specified search pattern.


2 Answers

Try this:

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

This uses the \1 backreference to match the text of first capturing group (\w+).

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

like image 172
Hokascha Avatar answered Sep 21 '22 10:09

Hokascha


Should be with global!

/(\w+)\.\1/g;
like image 27
TomTom Avatar answered Sep 23 '22 10:09

TomTom