php - 字符串到正则表达式的转换
问题描述
(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",
],
]';
推荐阅读
- sql - 如何选择具有数字作为值的行?
- url - DNN 中的 URL 重定向问题
- java - 如何向 EditTex 添加过滤器?
- javascript - 如何在 Oracle JET 中配置 SCSS?
- python - 有没有办法在框架中的任何地方放置一个 tkinter Checkbutton?
- c# - CsvHelper PrepareHeaderForMatch 将 Context 作为一项数组返回
- c# - C# 和 Angular 之间的压缩方案或 Angular 7 中的 zip 功能
- .net - 如何在现有的 Rtf 文档中引入新的格式说明符
- c# - 从 System.Linq 导入类型如何将方法添加到其他类?
- c# - 简单灵活的撤销重做系统