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 ?
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
^
line start (NP++ default)
\[
matches an opening bracket
.*+
any amount of any characters besides newline (without giving back)
\s*+
any amount of whitespace (also possessive to reduce backtracking)
(?!
negative lookahead to fail on the defined condition )
which is:
[^\[]
a character that is not an opening bracket
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.