【发布时间】:2014-07-23 01:33:28
【问题描述】:
我想像这样解析字符串:
[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+]来分割字符串。
我将如何为此编写正则表达式?
【问题讨论】:
-
所需的确切输出是什么?另外,您使用的是哪种语言的正则表达式?
-
为什么不使用
\[VP .+?\]? -
您几乎已经编写了正确的模式,您只需要转义左方括号,最终(取决于您的正则表达式风格)转义右方括号。 (正如耶特注意到的那样)
-
我正在使用 php,输出应该在数组中。
-
@user3700749:使用
preg_split,您就到达了:php.net/manual/en/function.preg-split.php(请记住,\w不像[\w\s]那样包含空格字符。)