I have a String containing zero or more numbers, then zero or more characters ABCD
. I want to parse the number into one group, then all characters into a separate group per character. I've tried:
([0-9]*)([ABCD])*
: Captures number correctly, but only the last letter
([0-9]*)(([ABCD])*)
: Captures number correctly, but then first all the letters in one group, then only the last letter in a group
I understand why each of those results happen, but I don't know how to fix it. How do I change my regex to give me multiple groups for the matched characters? Bonus points if I don't get an empty group (''
or undefined
) if either the number or the letters aren't there.
For example:
1A
=> [1, A]
99
=> [99]
CAB
=> [C, A, B]
1234ABCD
=> [1234, A, B, C, D]