1

Looking for a regex to capture all quoted key-value pairs from a log line (exim, to be precise). However, the value can also contain quotes

Tried multiple examples from other SO questions (eg Regular expression to match key-value pairs where value is in quotes or apostrophes), all have failed as soon as they hit an internal quote character

Test data, all valid examples:

A="value" F="something here" T="This is the value"
F="something \"here\"" T="The value's here" X=ignore
F="something's here" T="He said \"This is also the value\""
anubhava
  • 761,203
  • 64
  • 569
  • 643
Jon T
  • 118
  • 5
  • You can have a look at [this post](https://stackoverflow.com/questions/171480/regex-grabbing-values-between-quotation-marks) to see if it helps. You might need to combine it with the one from the other example. – M. Eriksson Sep 11 '19 at 14:48

1 Answers1

2

You may use this regex to match all individual key-value pairs:

(?<key>[^\s=]+)=(?<value>"[^"\\]*(?:\\.[^"\\]*)*"|[^\s"]+)

RegEx Demo

RegEx Details:

  • (?<key>[^\s=]+): Match 1 or more characters that are not = and whitespaces, capture in group "key"
  • =: Match a literal =
  • (?<value>: Start capture group "value"
    • "[^"\\]*(?:\\.[^"\\]*)*": Match a quoted string ignoring all escaped character inside the "..."
    • |: OR
    • [^\s"]+: Match 1 or more characters that are not = and whitespaces
  • ): End capture group "value"
anubhava
  • 761,203
  • 64
  • 569
  • 643