Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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 Regexp lookahead和lookahead以及某些字符之间的匹配_Php_Regex_Match - Fatal编程技术网

Php Regexp lookahead和lookahead以及某些字符之间的匹配

Php Regexp lookahead和lookahead以及某些字符之间的匹配,php,regex,match,Php,Regex,Match,目前,我有这个regexp来检测双花括号之间的字符串,它工作得非常好 $str = "{{test}} and {{test2}}"; preg_match_all('/(?<={{)[^}]*(?=}})/', $str, $matches); print_r($matches); Returns: Array ( [0] => Array ( [0] => test [1] => test2 ) ) 我一直在尝试修

目前,我有这个regexp来检测双花括号之间的字符串,它工作得非常好

$str = "{{test}} and {{test2}}";
preg_match_all('/(?<={{)[^}]*(?=}})/', $str, $matches);
print_r($matches);

Returns:
Array
(
[0] => Array
    (
        [0] => test
        [1] => test2
    )

)
我一直在尝试修改正则表达式,但是向前看和向后看对我来说太难了。我怎样才能让它匹配里面的东西]]和[[仅限]

我还想匹配]]和[[之间的整个字符串,然后我想匹配其中{{}}之间的每个单独字符串

例如:

$str = "{{dont match}}]]{{test}} and {{test2}}[[{{dont match}}";

Would return:
Array
(
[0] => Array
    (
        [0] => {{test}} and {{test2}}
        [1] => test
        [2] => test2
    )

)

使用
preg\u replace\u回调进行搭载

$str = "{{dont match}}]]{{test}} and {{test2}}[[{{dont match}}";
$arr = array();
preg_replace_callback('/\]\](.*?)\[\[/', function($m) use (&$arr) {
            preg_match_all('/(?<={{)[^}]*(?=}})/', $m[1], $arr); return true; }, $str);
print_r($arr[0]);

请发布你用过的修改过的正则表达式。它不起作用,我试着添加\]\]和\[\[在(?后退一步。你不能先匹配
]]..[
(使用你发布的稍微修改过的正则表达式),然后对第一个正则表达式的结果执行发布的正则表达式吗?@unska你的输入总是上面的格式吗?
$str = "{{dont match}}]]{{test}} and {{test2}}[[{{dont match}}";
$arr = array();
preg_replace_callback('/\]\](.*?)\[\[/', function($m) use (&$arr) {
            preg_match_all('/(?<={{)[^}]*(?=}})/', $m[1], $arr); return true; }, $str);
print_r($arr[0]);
Array
(
    [0] => test
    [1] => test2
)