2

Why does the pattern A[^A]+A only match once in AWERTYUIOPAZXCA?

Should be: AWERTYUIOPA and AZXCA.

https://regex101.com/r/Fd13U4/1

Daniel W.
  • 31,164
  • 13
  • 93
  • 151
Matvey
  • 25
  • 3
  • 1
    check out https://stackoverflow.com/questions/320448/overlapping-matches-in-regex/321391 – Marko Jun 22 '21 at 14:21

1 Answers1

3

Your regex matches the string

AWERTYUIOPAZXCA
|---------| 
A[^A]+    A

This is the only pattern in that string.

The remaining string doesn't match anymore, because it doesn't begin with A:

ZXCA
|
A...

If you change the string to: AWERTYUIOPAAZXCA it will match twice:

AWERTYUIOPAAZXC      A
|---------||---------|
A[^A]+    AA[^A]+    A

If you use a positive lookahead, you can assure to only match strings that end with an A:

((A[^A]+)(?=A))

This will give you AWERTYUIOP and AZXC - you need to tweak the regex if you want to capture the last character but it doesn't capture if the string/character (A) is not present.

Daniel W.
  • 31,164
  • 13
  • 93
  • 151