Search code examples
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:

    • \[: Match opening [
    • (?:: Start non-capture group 1
      • \d+: Match 0+ digits
      • (?:: Start non-capture group 2
        • ([;,]): Match ; or , and capture in group #1
        • \d+: Match 1+ digits
        • (?:\1\d+)*: Match \1 which is back-reference of capture group #1 followed by 1+ digits. Repeat this group 0 or more times
      • )?: End non-capture group 2. ? makes this group optional.
    • )?: End non-capture group 1. ? makes this group optional.
    • \]: Match closing ]