2017-06-17 72 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