match the same unknown character multiple times

后端 未结 4 1942
小蘑菇
小蘑菇 2021-01-01 21:41

I have a regex problem I can\'t seem to solve. I actually don\'t know if regex can do this, but I need to match a range of characters n times at the end of a pattern. eg. bl

4条回答
  •  一向
    一向 (楼主)
    2021-01-01 21:58

    You can use this pattern: blahblah([A-Z])\1+

    The \1 is a back-reference to the first capture group, in this case ([A-Z]). And the + will match that character one or more times. To limit it you can replace the + with a specific number of repetitions using {n}, such as \1{3} which will match it three times.

    If you need the entire string to match then be sure to prefix with ^ and end with $, respectively, so that the pattern becomes ^blahblah([A-Z])\1+$

    You can read more about back-references here.

提交回复
热议问题