regexregex-lookaroundsinifind-replaceregex-replace

Regex: Remove empty sections from INI (Text) files where the files could contain multiple such sections


I have below resultant php.ini file, after I was able to remove the comments & spaces thro' some simple Regex find/replace steps:

[PHP]
engine = On
short_open_tag = Off
....
....
[CLI Server]
cli_server.color = On
[Date]
[filter]
[iconv]
[imap]
[intl]
[sqlite3]
[Pcre]
[Pdo]
[Pdo_mysql]
pdo_mysql.default_socket=
[Phar]
[mail function]
SMTP = localhost
smtp_port = 25
mail.add_x_header = Off
[ODBC]
....
....
[dba]
[opcache]
[curl]
curl.cainfo = "{PHP_Dir_Path}\extras\curl\cacert.pem"
[openssl]
[ffi]

As you can see, there are multiple occurrences where multiple empty sections(sections which doesn't contain any semicolon-less(non-commented) lines) in this file, and I can't bring myself to make a regex find/replace pattern that could let me remove all such empty sections in one go, so that it becomes like below:

[PHP]
engine = On
short_open_tag = Off
....
....
[CLI Server]
cli_server.color = On
[Pdo_mysql]
pdo_mysql.default_socket=
[mail function]
SMTP = localhost
smtp_port = 25
mail.add_x_header = Off
[ODBC]
....
....
[curl]
curl.cainfo = "{PHP_Dir_Path}\extras\curl\cacert.pem"

Can anyone help me out achieve, what I need ?


Solution

  • An idea to look ahead after lines starting with [ for another opening bracket (or end).

    ^\[.*+\s*+(?![^\[])
    

    Here is the demo at regex101 - If using NP++ uncheck: [ ] . dot matches newline

    In short words it matches lines starting with [ up to eol and any amount of whitespace...
    if there is either no character ahead or the next character is another [ opening bracket.
    Side note: Its positive equivalent is ^\[.*+\s*+(?=\[|\z) where \z matches end of string.