0

I tried to explain the pattern below. The key-value pair parsing should succeed if the values are separated by a new line or a comma. But separators should not create empty values, like a comma at the beginning, at the end or two consecutive commas, etc. So that I could get only key-value pairs by just calling capture groups.

key:value # should succeed
key:value,key:value,key:value # should succeed, can be called $1, $2, $3, etc.
key:value, # should fail
,key:value # should fail
key:value,key:value,key:value, # should fail
key:value,,key:value,key:value # should fail

I tried something like below, but it matches only first and last ones, excluding the others in one line.

^(key:value)(?:,(key:value))+$

Any ideas to capture the repeating pairs?

Edit: The "key" and "value" have their own patterns and I have solved them already. I am struggling with repeating capture groups. Just to clarify.

Zafer Balkan
  • 176
  • 12
  • Hi @WiktorStribiżew, you are right. Removed the tag. – Zafer Balkan Oct 19 '22 at 14:14
  • The reader is utilizing pcre2grep, so I can only change the pattern, not the code. In this case, language itself does not matter. – Zafer Balkan Oct 19 '22 at 14:19
  • 1
    This is a site for programming issues only. The problem you get is easily fixed with programming methods. Repeated capturing groups always only keep the last captured value - that is by design. – Wiktor Stribiżew Oct 19 '22 at 14:22
  • Yes, there is a nice read about that https://www.regular-expressions.info/captureall.html – The fourth bird Oct 19 '22 at 14:23
  • What is the goal? To validate the line from start to end AND extract key and value of each pair by use of two capturing groups? How does the real data look like? Please try to improve the question. – bobble bubble Oct 19 '22 at 15:09
  • @WiktorStribiżew, thanks for the warning. I assumed regex pattern would count as valid. – Zafer Balkan Oct 19 '22 at 21:06
  • @Thefourthbird , IIRC I saw that page during my search but I could not make it then. I'll reread to check if I missed or misunderstood something. – Zafer Balkan Oct 19 '22 at 21:10
  • @bobblebubble , sorry for the oversimplified question. I was just trying to solve the part I was stuck as I managed to solve other patterns. – Zafer Balkan Oct 19 '22 at 21:10

1 Answers1

1

Adding a simple OR-Block to your regex seems to do the job for your provided example:

^(key:value)(?:,(key:value))+$|^key:value$

Also, I can suggest regex101.com, It's by far the best tool to use for regex debugging!

noah
  • 312
  • 2
  • 13