2017-09-13 89 views
-2

我想创建一个正则表达式,这将使我出现,我使用\[start-flashcards\](.*?)\[end-flashcards\][start-flashcards][end-flashcards]之间PHP正则表达式的内容用方括号

但这种不匹配的一切。我肯定错过了什么?

<p>[start-flashcards]</p> 
    <p>[London|This is the capital city of the United Kingdom]</p> 
    <p>[Paris|This is the capital city of France]</p> 
    <p>[Madrid|This is the capital city of Spain]</p> 
    <p>[Tokyo|This is the capital city of Japan]</p> 
    <p>[Moscow|This is the capital city of Russia]</p> 
<p>[end-flashcards]</p> 

回答

0

你需要这样的:

\[start-flashcards\]([\s\S]*?)\[end-flashcards\]

您已经使用.,不匹配的换行符。

编辑:

原来有实现相同的有效途径:

使用您正则表达式\[start-flashcards\](.*?)\[end-flashcards\]/s修改标志。该标志允许.匹配换行符。

+0

不,不要使用'[\ s \ S]'。这是一个解决方法。使用'.'和'/ s'修饰符。 –

+0

当你说变通方法时,它是否有任何副作用? – CinCout

+1

内置'.'以更高效的方式更快地匹配。当然,我们没有注意到,但仍然... –