gpt4 book ai didi

php - 字符串到正则表达式的转换

转载 作者:搜寻专家 更新时间:2023-10-31 21:20:44 25 4
gpt4 key购买 nike

(name eq "xxxx" and user:(post.id eq 6 and post.post eq "testing") and data eq "Hello World!")

我有上面的字符串,我想通过 RegEx 过滤它以获得以下输出:

[
[
"name eq "xxxx"",
"user" => [
"post.id eq 6",
"post.post eq testing",
],
"data eq "Hello World!"",
],
[
"name",
"user" => [
post.id,
post.post
],
"data",
],
]

我尝试了以下正则表达式解决方案

/([\\w\\.]+)[\\s]+(?:eq)[\\s]+(?:"(?:[^"\\\\]|\\\\.)*"|\\d+(?:,\\d+)*(?:\\.\\d+(?:e\\d+)?)?|null)/i

并得到值

[
[
"name eq "xxxx"",
"post.id eq 6",
"post.post eq "testing"",
],
[
"name",
"post.id",
"post.post",
],
]

最佳答案

试试这个,虽然我不检查正则表达式

$input="(name eq \"xxxx\" and user:(post.id eq 6 and post.post eq \"testing\") and data eq \"Hello World!\")";

$inarr = [":(", ")","("];
$reparr = [" and ", "", ""];
$input=str_replace($inarr, $reparr, $input);

$resultarr = explode(" and ", $input);

echo '[
[
$resultarr[0]' . ',' .
'"' . $resultarr[1] . '" => [' .
$resultarr[2] . ',' .
$resultarr[3] . ',' .
'],
"' . $resultarr[4] . '",
],
[
"name",
"user" => [
post.id,
post.post
],
"data",
],
]';

关于php - 字符串到正则表达式的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50230909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com