使用正则表达式以纯文本格式获取列表

时间:2013-04-14 14:35:51

标签: php regex text-processing plaintext

我想使用正则表达式以纯文本(.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。但我从那种模式中得不到任何东西。有谁知道我应该使用什么模式?

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

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

preg_match_all('/^[0-9]+\. .*$/m', $list, $matches);
$result = $matches[0];