-1

我尝试匹配 PHP 数组的所有数组值——它们没有被引用,但它有一个不能正常工作的多行值(选项)。

我的数组是:

[
    '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'),
        ]) : [],
    ],
]

那是正则表达式:=>\s+([^'].*[^'][\s\S]+?)(\]\)\ \:\ \[\]|),\n

这是我的测试:https ://regex101.com/r/gWWx7y/1

另一种方法是使用另一个正则表达式对数组进行预处理,以删除所有不需要的换行符,除了符合 (=> 的换行符和带有,) 的换行符,但我不知道我该怎么做。

4

1 回答 1

0

你可以试试:

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

分解后,内容如下:

(?:                # 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

但我永远不会在任何生产系统中使用它。在 regex101.com 上查看演示

于 2021-11-04T21:33:16.460 回答