Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php正则表达式中,如何基于分隔符不在两个字符之间来拆分字符串?_Php_Regex_Perl_Google Maps_Delimiter - Fatal编程技术网

在php正则表达式中,如何基于分隔符不在两个字符之间来拆分字符串?

在php正则表达式中,如何基于分隔符不在两个字符之间来拆分字符串?,php,regex,perl,google-maps,delimiter,Php,Regex,Perl,Google Maps,Delimiter,我试过很多东西,但都不管用。我有一个字符串,我想用逗号拆分字符串,,,但如果逗号位于括号、大括号或双引号或单引号之间,则不拆分-在这种特定情况下,可以维护完整的iframe(这会破坏所有内容)字符串。下面是字符串 class : putmeincoach, id : random_id, responsive : { [ type= text ; html = "< iframe width="425" height="350" frameborder="0"

我试过很多东西,但都不管用。我有一个字符串,我想用逗号拆分字符串,,,但如果逗号位于括号、大括号或双引号或单引号之间,则不拆分-在这种特定情况下,可以维护完整的iframe(这会破坏所有内容)字符串。下面是字符串

class : putmeincoach, 
 id : random_id,  
   responsive : {   [ type= text ;  html = "< iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps/myplaces?ctz=480&ie=UTF8&ll=34.009394,-118.488514&spn=0.020687,0.020921&t=m&z=16"> < /iframe >" ;  ]  }
我只是想

Array (
   [0] = 'class : putmeincoach',
   [1] = 'id : random_id',
   [2] = 'responsive : {   [ type= text ;  html = "< iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps/myplaces?ctz=480&ie=UTF8&ll=34.009394,-118.488514&spn=0.020687,0.020921&t=m&z=16"> < /iframe >" ;  ]  } '
我可以使用preg_split或preg_match与一些广义正则表达式模式进行匹配吗?Lemme还提到,在PHP中,双引号之间没有逗号是最重要的:

<?php
$input = '
    class : putmeincoach, 
    id : random_id, 
    responsive : {   [ type= text ;  html = "&lt; iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps/myplaces?ctz=480&amp;ie=UTF8&amp;ll=34.009394,-118.488514&amp;spn=0.020687,0.020921&amp;t=m&amp;z=16"&gt; &lt; /iframe &gt;" ;  ]  }, 
    testsinglequotes: \'hello world\', 
    testdoublequotes: "hello hello"';
$pattern = '/ 
    \b(\w+)\s*:   # capture the word before the colon 
    \s*(          # start capture group match after the colon
        {[^}]*}\s*|     # match everything between braces  OR
        "[^"]*"\s*|      # match everything between double quotes OR
        \'[^\']*\'\s*|   # match everything between single OR
        [^,]*           # match everything up to the comma 
        )         # end capture group
    (?:,|$)       # match comma or end of string (non-capture group) 
    /x';
preg_match_all($pattern ,$input, $matches);
print_r($matches);
?>
Regex是您在PHP中的朋友:

<?php
$input = '
    class : putmeincoach, 
    id : random_id, 
    responsive : {   [ type= text ;  html = "&lt; iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps/myplaces?ctz=480&amp;ie=UTF8&amp;ll=34.009394,-118.488514&amp;spn=0.020687,0.020921&amp;t=m&amp;z=16"&gt; &lt; /iframe &gt;" ;  ]  }, 
    testsinglequotes: \'hello world\', 
    testdoublequotes: "hello hello"';
$pattern = '/ 
    \b(\w+)\s*:   # capture the word before the colon 
    \s*(          # start capture group match after the colon
        {[^}]*}\s*|     # match everything between braces  OR
        "[^"]*"\s*|      # match everything between double quotes OR
        \'[^\']*\'\s*|   # match everything between single OR
        [^,]*           # match everything up to the comma 
        )         # end capture group
    (?:,|$)       # match comma or end of string (non-capture group) 
    /x';
preg_match_all($pattern ,$input, $matches);
print_r($matches);
?>
Regex是你的朋友

我试过了

^(class.*?,).*?(id.*?,).*?(responsive.*,?)$
使用,这是有效的。棘手的事情是在前两个组中为。*使用惰性匹配,在最后一个组中为“.”使用惰性匹配

我认为,由于正则表达式的处理方式有点不同,您需要将其适应于PHP。

我尝试过

^(class.*?,).*?(id.*?,).*?(responsive.*,?)$
使用,这是有效的。棘手的事情是在前两个组中为。*使用惰性匹配,在最后一个组中为“.”使用惰性匹配



我认为,由于正则表达式的处理方式有点不同,您需要将其应用到PHP中。

每个逗号后面是否有一行新行?嘿,谢谢您的编辑。顺便问一下,你是怎么做到的?我该如何学习?此外,NHAHDH-新行与匹配无关。我只是为了让人们看问题的可读性而把它们分开。@JoeMcmorgan,在你输入问题文本的框中,看看上面的按钮,他们很欣赏NHAHDH的提示,但我试图远离其他数据结构,比如JSON和php正则表达式。尽管如此,我还是无法检测到页面上解码的JSON字符串中包含逗号的示例。这不是JSON仅供参考,它只是我为它设置的一个结构,它以特定的方式接受用户输入。每个逗号后是否有新行?嘿,谢谢你的编辑。顺便问一下,你是怎么做到的?我该如何学习?此外,NHAHDH-新行与匹配无关。我只是为了让人们看问题的可读性而把它们分开。@JoeMcmorgan,在你输入问题文本的框中,看看上面的按钮,他们很欣赏NHAHDH的提示,但我试图远离其他数据结构,比如JSON和php正则表达式。尽管如此,我还是无法检测到页面上解码的JSON字符串中包含逗号的示例。这不是JSON供参考,它只是我为它设置的一个结构,它以特定的方式接受用户输入。有没有一种方法可以做到这一点而不包括“类”、“id”或“响应”?这些只是随机的关键字,根据输入的内容,我可能会有更多或更少的关键字(以及不同的关键字)。是的,请参见Hobo先生的答案;)有没有一种方法可以在不包含“类”、“id”或“响应”的情况下做到这一点?这些只是随机的关键字,根据输入的内容,我可能会有更多或更少的关键字(以及不同的关键字)。是的,请参见Hobo先生的答案;)哈哈,这真是太复杂了。有什么有用的方法来解释它的工作原理吗?我想没有比这更好的了+谢谢Hobo先生,但这在PHP中似乎不起作用,因为我必须将模式包装在“/和/”中,并且您在其中有没有转义的引号。这是我用旗子和其他东西放的。。preg\u match\u all(“/\b(\w+)\s*:\s*({[^}]*}\s*\\\“[^\]”]\“\s*”[^']'.\s*[^,]*)(?:,|$)/gm“,$searchme,$matches);它失败了,因为我不得不逃避引用。不过你之前在评论中提到过……你的答案仍然存在逃避问题,因此不起作用,但我会给你分数,因为它至少在你之前展示给我的链接上是有效的。哈哈,这太复杂了。有什么有用的提示来解释它是如何工作的吗?我想这没什么可赌的除此之外,我还有+1。谢谢Hobo先生,但这在PHP中似乎不起作用,因为我必须用“/”和/”来包装模式,并且你在其中有没有转义的引号。这就是我用标志和所有东西来放的东西。preg_match_all(/\b(\w+)\s*:\s*({[^}]*\s*”[^\\\\“\s*”[^']''.\s*[^'.[^'.[^']].[^,]*)(?:,[124$)/gm searchs],$me,$matches”);它失败了,因为我不得不逃避引用。你之前在评论中提到过……你的答案仍然有问题,因此不起作用,但我会给你分数,因为至少在你之前给我看的链接上是有效的。