-1

My Regex voodoo powers were not enough to beat this one up:

Input:

x|a=1|b=2,y|c=3|d=4,
(bonus point if you allow me to drop the last comma)

Desired matches (and groups):

x|a=1|y=2 (x, a, 1, y, 2)
y|c=3|d=4 (y, c, 3, d, 4)
(bonus point if I can name every group)

My failing pattern:

(x|y|z)(?:\|(.+?)=(.+?))*,

Basically, I want x, y or z and ALL the '|' separated pairs of 'this=that' afterwards.

Matches (and groups) I'm getting:

x|a=1|b=2, (x, b, 2)
y|c=3|d=4, (y, d, 4)

I have the comma in the matches, but that's no big deal, the main issue is that I'm only getting the last "sub-groups" for each match. Why is the first one ignored?

Kilazur
  • 3,089
  • 1
  • 22
  • 48
  • In .Net you can see repeated captures in Groups[?].Captures. Don't worry you already have correct regex and you can extract all data. Btw I think you can drop last comma – Kovpaev Alexey Aug 05 '16 at 14:37
  • Damn, I didn't think of captures. I guess I'm gonna change of testing website... – Kilazur Aug 05 '16 at 14:40
  • Is this your own format? As @KovpaevAlexey says, if each subsequence starts with `[a-z]\|`, you already don't need the comma. But if it's your own format, I would use JSON or XML instead. Or go Full State Machine and write it myself. But that's basically never justified any more, sad to say. – 15ee8f99-57ff-4f92-890c-b56153 Aug 05 '16 at 14:51

1 Answers1

1

It's nice feature of .Net Regex to capture repeating groups and there are already pretty much answers on similar questions so I'll just put the link below

https://stackoverflow.com/a/11051948/1009099

Community
  • 1
  • 1
Kovpaev Alexey
  • 1,725
  • 6
  • 19
  • 38