Regexp for capturing matched matches

I am looking for a regexp parameter or trick for capturing all possible strings in a regex where matches might overlap.

Example: /A.A/

in line"ABACADA"

He finds:, ABA, ADA

not ACA

!!

I would like: ABA, ACA, ADA

I work in PHP but it can be applied to other languages

preg_match_all('/A.A/',"ABACADA",$matches);
var_dump($matches[0]);
// output : array (size=2)
// 0 => string 'ABA' (length=3)
// 1 => string 'ADA' (length=3)

      

Could you help me? Thanks to

+3


source to share


1 answer


You can use a positive lookback to get all 3 matches:

(?=(A.A))

      

Demo version of RegEx



For your input, it finds 3 matches:

  • ABA

  • ACA

  • ADA

+2


source







All Articles