Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect repeated text

I'm making a discord bot using node.js and I wanted to add an anti spam feature to it. The goal is simple:

I would like to match, using regex preferably, the following sentence:

hello my name is anon hello my name is anon hello my name is anon hello my name is anon

Tho, I dont want it to match these strings:

hello my name is anon hello my name is anon hello my name is anon

hello my name is anon

hello, my name is anon hello my name is anon hello my name is anon hello my name is anon (notice the comma at the start)

Of course, the few words im using here could be anything and thats were I need your help... Is regex able to do this? If so, how? I took a look at words boundaries and stuff but im pretty lost here...

Thanks in advance! :)

like image 603
keanu_reeves Avatar asked Feb 12 '26 22:02

keanu_reeves


1 Answers

If you wish to identify strings of the form s s s sx, where s is a common substring and x is any substring, you could use the regular expression

^(.+)(?: +\1){3}

Demo

The regex engine performs the following operations.

^            match beginning of line
(.*+)        match any char other than newline, 1+ times,
             saving to capture group 1
(?: +\1)     match 1+ spaces followed by the content of
             capture group 1 in a non-cap group
{3}          execute non-cap group 3 times
like image 111
Cary Swoveland Avatar answered Feb 15 '26 10:02

Cary Swoveland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!