Search code examples
phpregexpcre

Parsing multiline ini like file using PCRE regex


I have an ini like file where we have list of <key> = <value> items. What complicates things is that some values are multiline and can contain = character (tls private key). Example:

groupid = foo
location = westus
randomkey = fbae3700c34cb06c
resourcename = example4-resourcegroup
tls_private_key = -----BEGIN RSA PRIVATE KEY-----
//stuff
-----END RSA PRIVATE KEY-----

foo = 123
faa = 223

What I have so far for pattern is this /^(.*?)\ \=\ (.*[^=]*)$/m and it works for all keys except the tls_private_key because it contains = so it only fetches partial value.

Any suggestions?


Solution

  • You might match all the values over mulitple lines, asserting that the next line does not contain a space equals sign space:

    ^(.*?) = (.*(?:\R(?!.*? = ).*)*)
    

    Regex demo

    If the key can not have spaces:

    ^([^\s=]+)\h+=\h+(.*(?:\R(?![^\s=]+\h+=\h+).*)*)$
    

    Explanation

    • ^ Start of string
    • ([^\s=]+) Capture group 1, match 1+ chars other than = or a whitespace char
    • \h+=\h+ Match an = between spaces
    • ( Capture group 2
      • .* Match the whole line
      • (?:\R(?![^\s=]+\h+=\h+).*)* Repeat all following lines that do not contain a space = space
    • ) Close capture group 2
    • $ End of string

    Regex demo