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

// output : array (size=2)
// 0 => string 'ABA' (length=3)
// 1 => string 'ADA' (length=3)


Could you help me? Thanks to


source to share

1 answer

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



Demo version of RegEx

For your input, it finds 3 matches:

  • ABA

  • ACA

  • ADA



All Articles