Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing multiline ini like file using PCRE regex

Tags:

regex

php

pcre

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?

like image 444
Darko Miletic Avatar asked Dec 31 '22 17:12

Darko Miletic


1 Answers

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

like image 138
The fourth bird Avatar answered Jan 08 '23 19:01

The fourth bird