繁体   English   中英

正则表达式:匹配未引用的 php 数组(多行)的值

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

我尝试匹配 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

另一种方法是用其他正则表达式预处理数组以删除所有不需要的换行符,除了符合 (=> 和行尾为,的换行符,但我不知道如何做到这一点。

你可以试试:

(?:=>\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 上查看演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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