Why does the pattern A[^A]+A
only match once in AWERTYUIOPAZXCA
?
Should be: AWERTYUIOPA
and AZXCA
.
Why does the pattern A[^A]+A
only match once in AWERTYUIOPAZXCA
?
Should be: AWERTYUIOPA
and AZXCA
.
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.