简体   繁体   中英

Regex: match values of php array (multiline) which are not quoted

I try to match all array values of a php array - which are not quoted, but it has a multiline value (options) which is not work correctly.

My array is that:

[
    'mysql' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],
    ],
]

That is the regex: =>\\s+([^'].*[^'][\\s\\S]+?)(\\]\\)\\ \\:\\ \\[\\]|),\\n

Here is my test: https://regex101.com/r/gWWx7y/1

A alternative is to preprocess the array with a other regex to remove all unneeded newlines, except the newlines which are in line with (=> and a line-end with , ) but I don't know how I can do this.

You may try:

(?:=>\s*(?:\[|'[^']*')(*SKIP)(*FAIL))|=>\s*\K.+

Broken down, this reads:

(?:                # non-capturing group
    =>\s*          # => plus whitespace, eventually
    (?:            # another non-capturing group
        \[         # a "[" literally
        |          # or
        '[^']*'    # some quoted string
    )
    (*SKIP)(*FAIL) # ignore all of the above
 )
 |                 # or
 =>\s*\K.+         # possibly unquoted things

But I'd never use it in any productive system. See a demo on regex101.com .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM