[英]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.