2017-06-17 74 views
0

我目前努力下面的“类型”的内容相匹配:如何匹配所有“类型”并正确地转义正则表达式?

(Type): multiplechoice 
(Category): 0 
(Random answers): 0 
(Question): Which of the following is true about pre-test imagery? 

(Type): multiplechoice 
(Category): 0 
(Random answers): 1 
(Question): Which of the following is not true about the TMJ? 

我想:

preg_match_all("(Type)\:(.+?)\n", $content, $types); 

但我得到一个未知的修饰词“\”的错误消息。

如何正确匹配所有类型?感谢任何提示!

+1

的可能的复制[警告:预浸\ _replace():未知的修饰词 '\]'(https://stackoverflow.com/questions/ 20705399/warning-preg-replace-unknown-modifier) – revo

+1

你需要在PHP中使用分隔符和正则表达式。 http://php.net/manual/en/regexp.reference.delimiters.php你还需要转义特殊的正则表达式字符'()'。 – chris85

回答

1

这应该做的,假设有空间后:

preg_match_all('/\((.+?)\): (.*)?/i', $subject, $array, PREG_PATTERN_ORDER); 
+0

这不会找到'Type's only。你也应该解释你做了什么('i'修饰符没有用处,因为没有字母字符)。 – chris85

+1

我对匹配关键字不区分大小写,它查找二进制数组中的类型和内容 –

+0

''''''无论'i'如何''''''''''都会与'.'匹配。 – chris85