【发布时间】:2014-06-13 11:56:05
【问题描述】:
我正在尝试从段落中提取句子,使用类似的模式
Current. time is six thirty at Scotland. Past. time was five thirty at India; Current. time is five thirty at Scotland. Past. time was five thirty at Scotland. Current. time is five ten at Scotland.
当我使用正则表达式时
/current\..*scotland\./i
这匹配所有字符串
Current. time is six thirty at Scotland. Past. time was six thirty at India; Current. time is five thirty at Scotland. Past. time was five thirty at Scotland. Current. time is five ten at Scotland.
相反,我想在第一次出现“。”时停止。到所有捕获组,如
Current. time is six thirty at Scotland.
Current. time is five ten at Scotland.
类似的文字
Past. time was five thirty at India; Current. time is six thirty at Scotland. Past. time was five thirty at Scotland. Past. time was five ten at India;
当我使用正则表达式时
/past\..*india\;/i
这将匹配整个字符串
Past. time was five thirty at India; Current. time is six thirty at Scotland. Past. time was five thirty at Scotland. Past. time was five ten at India;
在这里我想捕获所有组或第一组,如以下,以及如何在第一次出现“;”时停止
Past. time was five thirty at India;
Past. time was five ten at India;
如何使正则表达式停在“,”或“;”有上面的例子吗?
【问题讨论】:
-
你的模式是贪婪的,会尽可能匹配。通过附加
?使其变得懒惰,因此它看起来像/current\..*?scotland\./i。
标签: ruby regex ruby-on-rails-3 nlp