简体   繁体   English

如何将json嵌套在双方括号中?

[英]How to get json nested in double square brackets?

Here is my sample: 这是我的示例:

dummy json: {json here: "asdas"}
[[table
  {json here: "asdas"}
]]
[[pre 
  {json here: "asdasx"}
]]
[[text {json here: "red"} ]]

and I want output like below: 我想要如下输出:

{json here: "asdas"}
{json here: "asdasx"}
{json here: "red"}

UPDATE the json strings may contain curly brackets. 更新 json字符串可能包含大括号。

I just want to get all of the json strings, but I keep failing. 我只想获取所有json字符串,但我一直失败。
I've tried using #\\[\\[(table|pre|text).+({.*?}).+\\]\\]#s but I got the following output: 我尝试使用#\\[\\[(table|pre|text).+({.*?}).+\\]\\]#s但得到以下输出:

array(3) {
  [0]=>
  array(1) {
    [0]=>
    string(126) "[[table
      {json here: "asdas"}
    ]]
    [[pre 
      {json here: "asdasx"}
    ]]
    [[text {json here: "red"} ]]"
  }
  [1]=>
  array(1) {
    [0]=>
    string(5) "table"
  }
  [2]=>
  array(1) {
    [0]=>
    string(18) "{json here: "red"}"
  }
}

and btw I'm using php syntax preg_match_all to do above test. 顺便说一句,我正在使用php语法preg_match_all进行上述测试。

I was able to get your code working by altering your regex to this: 我可以通过将正则表达式更改为此来使您的代码正常工作:

\[\[(?:table|pre|text)\s*(\{.*?\})\s*\]\]

Note carefully that brackets need to be escaped if you intend for them to be literal; 请注意,如果要使它们成为文字,则需要将方括号转义; you weren't doing this in the regex you showed us. 您不是在向我们展示的正则表达式中执行此操作。

Code: 码:

$userinfo = "[[table  {json here: \"asdas\"}]] [[pre {json here: \"asdasx\"}]] [[text {json here: \"red\"} ]]";
preg_match_all ("/\[\[(?:table|pre|text)\s*(\{.*?\})\s*\]\]/", $userinfo, $pat_array);
print $pat_array[1][0]." <br> ".$pat_array[1][1]." <br> ".$pat_array[1][2];

Output: 输出:

{json here: "asdas"} <br> {json here: "asdasx"} <br> {json here: "red"}

Demo here: 演示在这里:

Rextester 右旋酯

This is the fastest and simplest pattern: \\[\\[\\S+\\s+\\K{.*} ( Pattern Demo ) 这是最快,最简单的模式: \\[\\[\\S+\\s+\\K{.*}模式演示

Explanation: 说明:

\[\[  #Match 2 opening square brackets
\S+   #Match 1 or more non-white-space characters
\s+   #Match 1 or more white-space characters
\K    #Start the fullstring match from this point (avoiding capture group)
{.*}  #Greedily match 0 or more non-line-terminating characters wrapped in curly brackets

*The curly brackets do not need to be escaped in my pattern because they are not mistaken for quantifiers. *大括号不需要在我的模式中转义,因为它们不会误认为量词。

Given the input value in my code to follow ( $in ), my pattern takes just 33 steps. 给定我代码中要遵循的输入值( $in ),我的模式仅需33个步骤。 Tim's pattern takes 116 steps and uses a capture group which makes preg_match_all() 's output array twice as big. 蒂姆的模式花了116步,并使用了一个捕获组,该捕获组使preg_match_all()的输出数组大了两倍。 inarilo's pattern takes 125 steps and uses a capture group. inarilo的模式需要125步并使用一个捕获组。

If anyone particularly wants to have a capture group, this can be used: /\\[\\[\\S+\\s+({.*})/ costing just 36 steps. 如果有人特别想要一个捕获组,则可以使用:/ /\\[\\[\\S+\\s+({.*})/ .* /\\[\\[\\S+\\s+({.*})/仅花费36个步骤。

Code ( PHP Demo ): 代码( PHP演示 ):

$in='dummy json: {json here: "asdas"}
[[table
  {json here: "asd{as}"}
]]
[[pre 
  {json here: "asdasx"}
]]
[[text {json here: "red"} ]]';

echo implode('<br>',(preg_match_all('/\[\[\S+\s+\K{.*}/',$in,$out)?$out[0]:[]));

Output: 输出:

{json here: "asd{as}"}<br>{json here: "asdasx"}<br>{json here: "red"}

Try this regex: 试试这个正则表达式:

#^\[\[(?:table|pre|text)\s+(\{.*?\})\s+\]\]$#m

Removed the global modifier since you are using preg_match_all. 由于您使用的是preg_match_all,因此删除了全局修饰符。

Following should work: 以下应该工作:

(\[\[[(table|pre|text) ]*[\n ].*)({.*})

https://regex101.com/r/Yv67gb/1 https://regex101.com/r/Yv67gb/1

This narrow down the sample to start with [[table or [[pre or [[text and then start the json with { which ends with } containing and text in between. 这样可以缩小样本的范围,使其以[[table[[pre[[text开头,然后以{}结尾的json开头,其中包含,文本之间。

Group 2 will be our result. 第2组将是我们的结果。

{json here: "asdas"}
{json here: "asdasx"}
{json here: "red"}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM