1

我是正则表达式的新手,并且遇到了一些问题。

我有一个文本文件,其中包含要放入数组的文本块。每个文本块都以“Hand #”开头,以“** Deck **”结尾,并用换行符分隔。它看起来像这样:

Hand #...random digits
blah
blah
blah
blah
** Deck **...random digits

Hand #...random digits
blah
blah
blah
blah
** Deck **...random digits

Hand #...random digits
blah
blah
blah
blah
** Deck **...random digits

我希望每个数组元素都包含每对“Hand #”和“** Deck **”之间的文本。到目前为止我尝试过的没有返回任何结果:

preg_match_all("%Hand #(.*?)[*][*] Deck [*][*]%", $file, $array);

谢谢您的帮助!

4

1 回答 1

3

. by default does not match line breaks. You can change this with the s (singleline or "dotall") modifier:

preg_match_all("%Hand #(.*?)[*][*] Deck [*][*]%s", $file, $array);

If you don't want your result to contain Hand # and ** Deck **, an alternative to capturing would be lookarounds:

preg_match_all("%(?<=Hand #).*?(?=[*][*] Deck [*][*])%s", $file, $array);

If you do want them, you can simply remove the parentheses from the first pattern.

于 2013-08-25T17:48:44.377 回答