regex 如何使用正则表达式提取多行字符串中的一段?

4nkexdtk  于 2023-08-08  发布在  其他
关注(0)|答案(2)|浏览(69)

我有一个多行字符串,我试图提取,但它没有返回任何片段。

$input = '<script>
buffy = 1;
vampire = {
response: [{"sleep":"night"}]
};
</script>';

preg_match( '~response:(.*?)};~', $input, $output );

print_r( $output );

字符串
我正在尝试提取[{"sleep":"night"}]

vxqlmq5t

vxqlmq5t1#

看看这个

$input = '<script>
buffy = 1;
vampire = {
response: [{"sleep":"night"}]
};
</script>';

preg_match('/response:\s*([^;]+);/', $input, $output);

print_r($output[1]);

字符串

ffx8fchx

ffx8fchx2#

'~response: (\[\{[^}]+}])~'

response    interpret as text
:           interpret as text
            interpret as text (one space)
(           start of capturing group
\[          interpret as text
\{          interpret as text
[^}]        anything up to the next }
+           ... and do this one or more times
}           interpret as text
]           interpret as text
)           end of capturing group

字符串

相关问题