大家好我正在尝试为PHP创建一个正则表达式,这将允许我获得引用和作者。如果一切都是一行,我已经使它工作了,但是当它放在多行中它停止工作的那一刻。我究竟做错了什么?
(\[quote\])(.*)(\|)(.*)(\[\/quote\])
[quote]沉默在船上,沉默在街上,The Locks and Haganport的码头会在你睡觉的时候抢劫你。| - 流行的孩子们 韵[/报价]
沉默你睡觉。| - 流行儿童的韵[/ quote]
答案 0 :(得分:4)
使用s
modifier(如果在正则表达式分隔符后附加)。另外,通过添加.*
:
?
非贪婪
preg_match_all("~\[quote\](.*?)\|(.*?)\[/quote\]~s", $s, $results, PREG_SET_ORDER);
输出位于$results
:
[
[
"[quote]Silence in the boatyard, Silence in the street, The Locks and Quays of Haganport Will rob you while you sleep.|-popular children's rhyme[/quote]",
"Silence in the boatyard, Silence in the street, The Locks and Quays of Haganport Will rob you while you sleep.",
"-popular children's rhyme"
], [
"[quote]Silence you sleep.|-popular children's rhyme[/quote]",
"Silence you sleep.",
"-popular children's rhyme"
]
]
答案 1 :(得分:3)
\[quote\](.*?)\|-(.*?)\[\/quote\]
注意:上面的正则表达式使用s
修饰符。或者,您可以将.
替换为[\s\S]
并停用s
修饰符
$re = '/\[quote\](.*?)\|-(.*?)\[\/quote\]/s';
$str = '[quote]Silence in the boatyard, Silence in the street, The Locks and Quays of Haganport Will rob you while you sleep.|-popular children\'s rhyme[/quote]
[quote]Silence you sleep.|-popular children\'s rhyme[/quote]';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
[quote]沉默在船上,沉默在街上,The Locks and Haganport的码头会在你睡觉的时候抢劫你。| - 流行的孩子们 韵[/报价]
沉默你睡觉。| - 流行儿童的韵[/ quote]
以下输出按组(由换行符分隔)
组织Silence in the boatyard, Silence in the street, The Locks and Quays of Haganport Will rob you while you sleep.
popular children's rhyme
Silence you sleep.
popular children's rhyme
\[quote\]
按字面意思匹配(反斜杠转义后续字符)(.*?)
任意次数捕获任何角色,但尽可能少捕获到捕获组1 \|-
按字面意思匹配(反斜杠转义后续字符)(.*?)
任意次数捕获任何字符,但尽可能少捕获到捕获组2 \[\/quote\]
按字面意思匹配(反斜杠转义后续字符)