kiamoz
kiamoz

Reputation: 714

regex match string has repeated pattern without any delimiter

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

Answers (3)

Ogod
Ogod

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

see example on regex101

Upvotes: 1

S.B
S.B

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

Admir Misini
Admir Misini

Reputation: 57

Use this regex: (\w+)\1

\1 matches the same text as most recently matched by the 1st capturing group

Upvotes: 2

Related Questions