regexstringregex-group

Regex match a string that is either seperated entirely by commas or seperated entirely by semicolons


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

    RegEx Explanation: