Reputation: 714
I wnat a regex to match staring has repeated pattern like this
abcabcabc // TRUE match is abc
mozmozmoz // TRUE match is moz
mozpmoz // false
thanks in advance
Upvotes: 0
Views: 124
Reputation: 948
I don't know if it is possible in a generic way with only using regex, because you have to describe the string you are searching for in some way.
However you can take this regex as a starting point:
^(\w+)\1+
^
means start of line
(\w+)
means at least one word character but up to unlimited, all captured in a group
\1
is a back reference to the capture group
+
means that the capture group must appear at least one time but can appear unlimited times
Upvotes: 1
Reputation: 16486
Try this one :
^(\w+)\1+$
The point is we use \1
to match exactly what matched in our first group which is (\w+)
.
click for demo:
Upvotes: 3
Reputation: 57
Use this regex:
(\w+)\1
\1
matches the same text as most recently matched by the 1st capturing group
Upvotes: 2