Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match/group repeating characters in a string

I need a regular expression that will match groups of characters in a string. Here's an example string:

qwwwwwwwwweeeeerrtyyyyyqqqqwEErTTT

It should match

(match group) "result"

(1) "q"

(2) "wwwwwwwww"

(3) "eeeee"

(4) "rr"

(5) "t"

(6) "yyyyy"

(7) "qqqq"

(8) "w"

(9) "EE"

(10) "r"

(11) "TTT"

after doing some research, this is the best I could come up with

/(.)(\1*)/g

The problem I'm having is that the only way to use the \1 back-reference is to capture the character first. If I could reference the result of a non capturing group I could solve this problem but after researching I don't think it's possible.

like image 207
user2936448 Avatar asked Oct 19 '25 01:10

user2936448


1 Answers

How about /((.)(\2*))/g? That way, you match the group as a whole (I'm assuming that that's what you want, and that's what's lacking from the solution you found).

like image 175
SQB Avatar answered Oct 22 '25 05:10

SQB