【问题标题】:How to match text in between two words(a..b) till the first occurence of the ending word(b)如何匹配两个单词之间的文本(a..b)直到第一次出现结束词(b)
【发布时间】:2014-08-20 18:50:03
【问题描述】:

谁能帮我从以下几行中匹配 From 和第一次出现的 Subject 之间的文本,

输入

Random Line 1
Random Line 2
From: person@example.com
Date: 01-01-2011
To: friend@example.com
   Subject: This is the subject line
Random Line 3
Random Line 4
   Subject: This is subject
This is the end

输出

From: person@example.com
Date: 01-01-2011
To: friend@example.com
   Subject: This is the subject line

我尝试了以下正则表达式,

/(From:.*(?i)Subject:.*?)\n/m

上面的正则表达式选择直到最后一个主题

【问题讨论】:

  • @AvinashRaj 我在正则表达式方面工作不多。你能详细解释一下dotall修饰符吗?

标签: ruby regex rubular


【解决方案1】:

这可行(请参阅:http://rubular.com/r/Lw9rhfwVGt):

/(From.*?Subject.*?)\n/m

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 2014-03-07
    • 1970-01-01
    相关资源
    最近更新 更多