[英]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.我尝试匹配 php 数组的所有数组值 - 这些值没有被引用,但它有一个无法正常工作的多行值(选项)。
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
那是正则表达式:
=>\\s+([^'].*[^'][\\s\\S]+?)(\\]\\)\\ \\:\\ \\[\\]|),\\n
Here is my test: https://regex101.com/r/gWWx7y/1这是我的测试: 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 .
在 regex101.com 上查看演示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.