Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.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

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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.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,我有一个脚本,需要从文本字符串中提取三个部分,然后以数组的形式返回它们。经过几次尝试和失败后,我无法让它工作 文本字符串可以如下所示: Some place Some place (often text in parenthesis) Some place (often text in parenthesis) [even text in brackets sometimes] $result = preg_match('/ ^ ([^(]+?) (\s* \( ([^)]++) \))

我有一个脚本,需要从文本字符串中提取三个部分,然后以数组的形式返回它们。经过几次尝试和失败后,我无法让它工作

文本字符串可以如下所示:

Some place Some place (often text in parenthesis) Some place (often text in parenthesis) [even text in brackets sometimes]
$result = preg_match('/
  ^ ([^(]+?)
  (\s* \( ([^)]++) \))?
  (\s* \[ ([^\]]++) \])?
  \s*
  $/x', $mystring, $matches);

print_r($matches);
某处 某些位置(通常为括号中的文本) 某些地方(通常是括号中的文字)[有时甚至括号中的文字] 我需要将这些字符串拆分为三个:

{Some place} ({often text in parenthesis}) [{even text i brackets sometimes}] {Some place}({通常是括号中的文本})[{有时甚至是文本i括号}] 应返回:

1: Some place 2: often text in parenthesis 3: even text in brackets sometimes 1:某个地方 2:通常是括号中的文本 3:有时甚至括号内的文本 我知道这应该是一项简单的任务,但我无法解决正确的正则表达式。这将在PHP中使用


提前谢谢

将问题分成三个正则表达式。在第一个括号之前的每个字符之后,保存您的位置-与刚才提取的字符串长度相同

然后在第二步中,做同样的事情,但是抓住所有东西直到结束括号。(嵌套的圆括号使其稍微复杂一些,但不会太多。)再次保存指向第二个字符串结尾的指针


获取第三个字符串很简单。

我可能会将其作为三个正则表达式,从括号和方括号开始,如果失败,则返回到较少的项

^(.*?)\s+\((.*?)\)\s+\[(.*?)\]\s+$
如果失败,请尝试:

^(.*?)\s+\((.*?)\)\s+$
如果同样失败,请尝试:

^\s+(.*?)\s+$

我确信它们可以组合成一个正则表达式,但我不会尝试。

尝试以下方法:

Some place Some place (often text in parenthesis) Some place (often text in parenthesis) [even text in brackets sometimes]
$result = preg_match('/
  ^ ([^(]+?)
  (\s* \( ([^)]++) \))?
  (\s* \[ ([^\]]++) \])?
  \s*
  $/x', $mystring, $matches);

print_r($matches);
请注意,在本例中,您可能对$matches[1]、$matches[3]和$matches[5]最感兴趣。

类似的内容

([^(]++)(?: \(([^)]++)\))?(?: \[([^\]]++)\])?

括号和括号的顺序总是一样的吗?