Flo
Flo

Reputation: 1207

Regex for multiline .ini file

I try to create a regex expression that can parse a ini-File.

But I want that the ini-values can be multiline!

Like that:

Wert1=Hallo
dsadasd
Wert2=Hi
Wert3=Heinirch Volland

I try it with this regex, but it doesn't work:

/.*=(.*)^.*=/gsm

Upvotes: 3

Views: 4626

Answers (3)

Aaron
Aaron

Reputation: 24812

You could be using this PCRE regex :

/^.*=.*[^=]*$/gm

Try it here.

This relies on the absence of the single-line flag, be careful not to set it. The multiline flag is also necessary, and global can be used if appropriate.

This matches from the start of a line containing an equal sign (^.*=.*), then will match as many whole lines that do not contain an equal sign as it can ([^=]*$, where [^=] will match linefeeds).

Upvotes: 3

Vinicius Luiz
Vinicius Luiz

Reputation: 132

I guess you are trying to get all ini values, and to do that you can use this regex pattern:

/^(.*)=(.*)/gm

and you'll can access your values using groups, each group will retrieve to you key and value

Upvotes: 1

Dan
Dan

Reputation: 10786

You appear to be using Perl. Have you considered using Config::IniFiles? That module will handle parsing INI-type files for you, and has support for multiline parameters using heredoc syntax:

Parameter=<<EOT
value/line 1
value/line 2
EOT

Or, if you enable it with Config::IniFiles->new(..., -allowcontinue => 1);, continuation lines:

[Section]
Parameter=this parameter \
  spreads across \
  a few lines

Upvotes: 0

Related Questions