ruby - 希望正则表达式在第一次出现 "."和 ";"时停止

标签 ruby regex ruby-on-rails-3 nlp

我正在尝试从段落中提取句子到,模式如下

 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; 

如何让正则表达式在“,”或“;”处停止有上面的例子吗?

最佳答案

有几件事你真的不应该用你的正则表达式来做,首先,正如 Arnal Murali 所指出的,你不应该使用贪婪的正则表达式,而应该使用惰性版本:

/current\..*?scotland\./i

我认为首先选择惰性选项是正则表达式的一般规则,因为它通常是您想要的。其次,您真的不想使用 . 来匹配所有内容,因为您不想让正则表达式的这一部分匹配 . 您可以将它们放在负捕获组中以捕获除它们之外的任何内容:

/current\.[^.]*?scotland\./i

/current\.[^;]*?india;/i

或同时覆盖:

/(current|past)\.[^.;]*?(india|scotland)[.;]/i

(显然这可能不是你想要做的,只是包括演示如何扩展它)

这也是一个很好的经验法则,如果您在使用正则表达式时遇到问题,请使任何通配符更具体(在这种情况下,从匹配所有 . 更改为匹配除 之外的所有内容。 ;[^.;])

关于ruby - 希望正则表达式在第一次出现 "."和 ";"时停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24204735/

相关文章:

ruby - 图形标题和替代文本中的 Pandoc 引用

python - 从文本中提取数字+空格/连字符和字母数字值

javascript - Rails 3 HTML 5/javascript 免费绘图推荐

javascript - 将 ruby​​ 数组转换为 javascript 数组

mysql - 使用大写 ID 从 Rails 应用程序保存 MYSQL 数据库中:Mysql2::Error: Field 'ID' 没有默认值

ruby-on-rails - 如何配置 action mailer(我应该注册域)?

c# - 字符串删除html

c - 如何在 POSIX 正则表达式中编写负向前瞻

ruby-on-rails - Rails Runner 抛出 : undefined local variable or method `app' for main:Object (NameError)

ruby-on-rails-3 - 环境与 WickedPDF 和 Heroku 冲突