Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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 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 \1 which 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 ]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading