Home > Software design >  How to write a regex to match a set of comma-separated key:value pairs?
How to write a regex to match a set of comma-separated key:value pairs?

Time:10-30

Say i want to match such a repeated pattern:

a:b
a:b,c:d

So a comma separated subsequence pairs s1:s2,s3:s4,.... (no trailing comma allowed), which each subsequence should be non-empty, and can include any ASCII char but not the separator: ,, and must match the whole string.

In this case how the rust regexp will look like?

CodePudding user response:

I would phrase the pattern as:

[^:,] :[^:,] (?:,[^:,] :[^:,] )*

Demo

  • Related