Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use a range in the number of occurrences while doing a regex match?

Tags:

python

regex

Given a string aaabbb is there a way I can write regex to find number of substrings like ab, aabb, aaabbb.

I was doing it by constructing a regex [a]{m}[b]{m} and the iterating over a range of values. But I would like to know if there is a way to do it in a single shot.

like image 918
Chaitanya Sama Avatar asked Jan 23 '26 00:01

Chaitanya Sama


1 Answers

As said in a comment, ^(?:a(?=a*(\1?+b)))+\1$ allows to match such balanced construct using wide spread regex functionality.

Demo

Full explanation here.

That being said if you want to list all overlaping substrings matching a balanced construct, you could use (?=((?:a(?=a*(\2?+b)))+\2)):

(?=                         # Using a lookahead allows to restart matching even if a match has already been found, as a lookaround doesn't "consume" its content.
  (                         # Using a capturing group allows to retrieve the match.
    (?:a(?=a*(\2?+b)))+\2)  # As an outer capturing group has been defined, thus numbered 1, we rewrite the inner part using the proper group 2.
  )
)

Demo

like image 172
PJProudhon Avatar answered Jan 26 '26 02:01

PJProudhon