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.
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.
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[.]*
The expression \w will match any word character. Word characters include alphanumeric characters ( - , - and - ) and underscores (_). \W matches any non-word character.
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.
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.
Should be with global!
/(\w+)\.\1/g;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With