Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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 - Fatal编程技术网

Php 正则表达式帮助-括号内的括号

Php 正则表达式帮助-括号内的括号,php,regex,Php,Regex,我正在尝试开发一个函数,它可以对如下所示的字符串进行排序: Donny went to the {park|store|{beach with friends|beach alone}} so he could get a breath of fresh air. 我打算做的是递归地搜索文本中的{}模式,在{}模式中没有{或},因此只选择最里面的三明治文本,然后运行php来排列内容并随机选择一个,重复这个过程,直到整个字符串被解析,显示完整的句子 不过,我就是不能完全理解正则表达式 谢谢你的

我正在尝试开发一个函数,它可以对如下所示的字符串进行排序:

Donny went to the {park|store|{beach with friends|beach alone}} so he could get a breath of fresh air. 
我打算做的是递归地搜索文本中的{}模式,在{}模式中没有{或},因此只选择最里面的三明治文本,然后运行php来排列内容并随机选择一个,重复这个过程,直到整个字符串被解析,显示完整的句子

不过,我就是不能完全理解正则表达式


谢谢你的帮助

正则表达式无法计数,因此无法可靠地找到匹配的括号

你需要的是一份工作


请参见此。

正则表达式无法计数,因此无法可靠地找到匹配的括号

你需要的是一份工作


看看这个。

你可以用词法分析器/解析器来完成这项工作。我不知道PHP中有任何选项(但因为PHP中有XML解析器,所以毫无疑问也有通用解析器)。另一方面,你要求做的事情并不太复杂。在PHP中使用字符串(子字符串等),您可能可以在几个递归函数中实现这一点


最后,您将使用简单的语法在PHP中创建一个MadLibz生成器。非常酷。

您可以使用lexer/解析器来实现这一点。我不知道PHP中有任何选项(但因为PHP中有XML解析器,所以毫无疑问也有通用解析器)。另一方面,你要求做的事情并不太复杂。在PHP中使用字符串(子字符串等),您可能可以在几个递归函数中实现这一点

$str="Donny went to the {park|store|{beach {with friends}|beach alone}} so he could get a breath of fresh air. ";
$s = explode("}",$str);
foreach($s as $v){
 if(strpos($v,"{")!==FALSE){
  $t=explode("{",$v);
  print end($t)."\n";
 }
}
最后,您将使用简单的语法在PHP中创建一个MadLibz生成器。很酷

$str="Donny went to the {park|store|{beach {with friends}|beach alone}} so he could get a breath of fresh air. ";
$s = explode("}",$str);
foreach($s as $v){
 if(strpos($v,"{")!==FALSE){
  $t=explode("{",$v);
  print end($t)."\n";
 }
}
输出

$ php test.php
with friends
输出

$ php test.php
with friends

不知道这背后的数学理论;-/但实际上这很容易。试一试

$text = "Donny went to the {park|store|{beach with friends|beach alone}} so he could get a breath of fresh air. ";

function rnd($matches) {
    $words = explode('|', $matches[1]);
    return $words[rand() % count($words)];
}

do {
    $text = preg_replace_callback('~{([^{}]+)}~', 'rnd', $text, -1, $count);
} while($count > 0);

echo $text;

不知道这背后的数学理论;-/但实际上这很容易。试一试

$text = "Donny went to the {park|store|{beach with friends|beach alone}} so he could get a breath of fresh air. ";

function rnd($matches) {
    $words = explode('|', $matches[1]);
    return $words[rand() % count($words)];
}

do {
    $text = preg_replace_callback('~{([^{}]+)}~', 'rnd', $text, -1, $count);
} while($count > 0);

echo $text;

正则表达式不能很好地处理递归内容,但PHP可以:

$str = 'Donny went to the {park|store|{beach with friends|beach alone}} so he could get a breath of fresh air.';

echo parse_string($str), "\n";

function parse_string($string) {
    if ( preg_match('/\{([^{}]+)\}/', $string, $matches) ) {
        $inner_elements = explode('|', $matches[1]);
        $random_element = $inner_elements[array_rand($inner_elements)];
        $string = str_replace($matches[0], $random_element, $string);
        $string = parse_string($string);
    }
    return $string;
}

正则表达式不能很好地处理递归内容,但PHP可以:

$str = 'Donny went to the {park|store|{beach with friends|beach alone}} so he could get a breath of fresh air.';

echo parse_string($str), "\n";

function parse_string($string) {
    if ( preg_match('/\{([^{}]+)\}/', $string, $matches) ) {
        $inner_elements = explode('|', $matches[1]);
        $random_element = $inner_elements[array_rand($inner_elements)];
        $string = str_replace($matches[0], $random_element, $string);
        $string = parse_string($string);
    }
    return $string;
}

不,他需要的是一个使用正则表达式的函数。没有必要用一个正则表达式来解决整个问题。对于这样一个简单的操作来说,正则表达式是过分了。以后省去一些麻烦,现在就“直接”写吧。不,他不需要语法,regexp正是这项工作的合适工具。见肯普和我的答案。不,他需要的是一个使用正则表达式的函数。没有必要用一个正则表达式来解决整个问题。对于这样一个简单的操作来说,正则表达式是过分了。以后省去一些麻烦,现在就“直接”写吧。不,他不需要语法,regexp正是这项工作的合适工具。看看肯普和我的答案。为什么投反对票?代码运行良好,回答了问题。向下投票。代码起到了作用(虽然我的更好;)为什么投反对票?代码运行良好,回答了问题。向下投票。代码对每个人都很友好(尽管我的代码更好)。问题已经解决了。@adbox:解决方案是什么?如果它是以下答案之一,您应该将其标记为已接受。如果不是答案之一,请自己写一个答案,以便有相同问题的人可以学习。要接受答案,请单击该答案旁边的复选标记图标。祝大家好运。问题已经解决了。@adbox:解决方案是什么?如果它是以下答案之一,您应该将其标记为已接受。如果不是答案之一,请自己写一个答案,以便有相同问题的人可以学习。要接受答案,请单击该答案旁边的复选标记图标。