Problem (not) greedy RegExp

Consider the following lines:

1: cccbbb

2: cccaaabbb

I would like to see such matches in the end:

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

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

How can I match both in one RegExp?
Here is my attempt:

#(aaa)?(.*)$#

I tried many options for greedy and jagged modifications, but that didn't work. As soon as I add '?' everything is agreed in [2]. Creating [2] bumps does not help.

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

+3
source share
5 answers
/(aaa)?((.)\3*)$/

However, it will be added [3]. I do not think the problem.

+3
source

Thanks for the brainstorming guys here! I finally could figure out what works:

^(?:([^a]*)(aaa))?(.*)$
+1

- . "aaa" , , "aaa" .

$str="cccaaabbb";
if (strpos($str,"aaa")!==FALSE){
   $array[]="aaa";
   $s = explode("aaa",$str);
   $array[]=end($s);
}
print_r($array);

$ php test.php
Array
(
    [0] => aaa
    [1] => bbb
)

[1], , "aaa" , , 4 strpos().

0

, , , . .

poweshell, .

( {3,3}) * ( {3,3})

0

:

$sPattern = "/(aaa?|)(bbb)/";

.

0

Source: https://habr.com/ru/post/1734777/


All Articles