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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With