php - 使用正则表达式分隔组中的字符串

标签 php regex pcre

我想像这样解析字符串:

[NP Amanda Brumfield],[NP the estranged daughter][PP of][NP actor Billy Bob Thornton],[VP was found][ADJP guilty][PP of][VP aggravated][NP manslaughter][PP of][NP a child]

在所有这种情况下,识别这些组:

[NP Amanda Brumfield][NP the estranged daughter][PP of][NP actor Billy Bob Thornton]
,
[ADJP guilty][PP of]
and
[NP manslaughter][PP of][NP a child]

换句话说,它应该使用字符串[VP\w+]来分割字符串。

我将如何为此编写正则表达式?

最佳答案

@Casimir et Hippolyte 提到使用 preg_split(),这是正确的。以下将按照您请求的方式拆分给定的输入:

$parts = preg_split('/\[VP\s+(?:[^\]])+\]/', $input);

该模式应该允许在初始 VP 之后的几乎所有内容以及在结束 ] 之前的一个或多个空格。 PHP 也应该适用于非捕获括号。

关于php - 使用正则表达式分隔组中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24002902/

相关文章:

正则表达式查找每个出现的字符串,并且它后面必须始终跟有相同的字符串

使用搜索语法解析搜索词的正则表达式模式

php - 使用 PHP 从字符串或文件中提取多个电话号码

php - 使用PHP流/缓冲大型音频文件

php - 循环遍历数组 mySQL insert

php - 错误 undefined variable : result + Error Invalid argument supplied for foreach() in CodeIgniter 2

sql - postgresql 9.5 : trying to split a string into two fields based on spaces

java - 使用 .contains 检测单词的多个实例

regex - 将 regexp_matches 结果存储在变量中

c++ - pcre2_substitute() 函数支持多少捕获组?