0

我想使用正则表达式获取纯文本(.txt 文件)中的所有列表项。例如在:

Books must I read this week before Saturday:
1. Geography
2. Math
3. Biology
The priority book is book 2. This book is borrowed by John.

我使用preg_match_all如下

$pattern = "/^[0-9]\.(.*)\n/";
preg_match_all($pattern, $filehandler, $matches);

我期待以下结果:

1. Geography
2. Math
3. Biology

该字符串2. This book is borrowed by John.不应在$matches. 但我没有从那种模式中得到任何东西。有谁知道我应该使用什么模式?

4

2 回答 2

1

你可以试试这个

$list = 'Books must I read this week before Saturday:
1. Geography
  2. Math
        3. Biology
The priority book is book 2. This book is borrowed by John.';

preg_match_all('/\n[\s\t]*(\d+\..*)/', $list, $bullets);

var_dump($bullets);
于 2013-04-14T14:43:53.650 回答
0

最有效的方法是使用多行模式:

preg_match_all('/^[0-9]+\. .*$/m', $list, $matches);
$result = $matches[0];
于 2013-04-14T15:03:09.743 回答