Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need a regular expression to match a substring n times [closed]

The regex .*{n} will match any single character n times, but I need to match any single substring n times.

How do I do that?

like image 809
fortesl Avatar asked Nov 20 '25 18:11

fortesl


2 Answers

To match the substring "foo" 3 times (for example "foofoofoo"), you could use the following:

(foo){3}

Or with a non-capturing group:

(?:foo){3}

As a side note, .*{n} wouldn't do what you think it does. The . will match any character, .* will match any number of any characters, and .*{n} will vary depending on the implementation but it will either be an invalid regex, be equivalent to .*, or match any number of any characters followed by the literal string '{n}'.

like image 149
Andrew Clark Avatar answered Nov 22 '25 06:11

Andrew Clark


Try

(your sub string here){n}

e.g.

(cats){4}
like image 27
Ed Heal Avatar answered Nov 22 '25 08:11

Ed Heal



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!