Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem (un-)greedy RegExp

Consider the following Strings:

1: cccbbb

2: cccaaabbb

I would like to end up with are matches like this:

1: Array
(
    [1] => 
    [2] => bbb
)

2: Array
(
    [1] => aaa
    [2] => bbb
)

How can I match both in one RegExp?
Here's my try:

#(aaa)?(.*)$#

I have tried many variants of greedy and ungreedy modifications but it doesn't work out. As soon as I add the '?' everything is matched in [2]. Making [2] ungreedy doesn't help.

My RegExp works as expected if I omit the 'ccc', but I have to allow other characters at the beginning...

like image 613
samy-delux Avatar asked Mar 14 '26 01:03

samy-delux


1 Answers

/(aaa)?((.)\3*)$/

There will be an extra [3] though. I don't think that's a problem.

like image 101
kennytm Avatar answered Mar 16 '26 05:03

kennytm



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!