What I’m trying to do is match a string of numbers that has a "consistent" separator between its contents. For example, I want is something that matches the following:
[1,2,3,20,4] &
[5;67;8;1;6]
but NOT this list
[1,2;4;5,6,7;8]
Is there any way of implementing this feature in regex?
What I have currently is
/\[(?> \d*[,;]) * \d* \]/gx
and while this matches the first two examples, it still matches the nonexample I gave.
>Solution :
You should use a capture group to capture first occurrence of ; or , and use the back-reference to make sure same delimiter is used until we match ] like this:
\[\d*(?:([;,])(?:\d+\1)*\d+)?\]
RegEx Demo:
\[: Match opening[\d*: Match 0+ digits(?:: Start non-capture group([;,]): Match;or,and capture in group #1(?:\d+\1)*: Match 1+ digits followed by\1which is back-reference of capture group #1. Repeat this group 0 or more times\d+: Match 1+ digits
)?: End non-capture group.?makes this group optional so that we match single element list as well.\]: Match closing]