简体   繁体   English

在ruby中提取单引号之间的日期

[英]Extract date between single quotes in ruby

I have a string like this: 我有一个像这样的字符串:

ticket:1 priority:5 delay:'2019-08-31 02:53:27.720422' delay:'2019-08-30 00:04:10.681242'

I successfully extracted ticket and priority but failed on delay . 我成功提取了ticketprioritydelay失败。

What I want is to extract delays as array so output will be like this: 我想要的是提取延迟作为数组,所以输出将是这样的:

#delays =>
[
  "delay:'2019-08-31 02:53:27.720422'",
  "delay:'2019-08-30 00:04:10.681242'"
]

What I've tried so far? 到目前为止我尝试过什么?

str = "ticket:1 priority:5 delay:'2019-08-31 02:53:27.720422' delay:'2019-08-30 00:04:10.681242'"
delays = str.scan(/delay:\w+(?:'\w+)*/).flatten

How can i extract them in my case? 我怎样才能在我的案例中提取它们? Note that, there is no guarantee that date format will be like in examples. 请注意,无法保证日期格式与示例类似。 Date format can be anything. 日期格式可以是任何内容。 So we should focus on strings between single quotes. 所以我们应该关注单引号之间的字符串。


If possible result can be like this (so that i dont have to extract date again.): 如果可能的结果可能是这样的(所以我不必再次提取日期。):

#delays =>
[
  "2019-08-31 02:53:27.720422",
  "2019-08-30 00:04:10.681242"
]

This expression might be close to what you have in mind: 这个表达式可能接近您的想法:

\bdelay\s*:\s*['][^']*[']

In case you had other chars such as " for the delay values, it would go in the char class: 如果您有其他字符,例如"对于delay值,它将进入char类:

\bdelay\s*:\s*['"][^'"]*['"]

or: 要么:

\bdelay\s*:\s*'(\d{4}-\d{1,2}-\d{1,2})\s*([^']*)'

Demo 2 演示2

or: 要么:

\bdelay\s*:\s*'(\d{4}-\d{1,2}-\d{1,2}\s*[^']*)'

Demo 3 演示3

or more simplified: 或更简化:

\bdelay\s*:\s*'([^']*)'

Test 测试

re = /\bdelay\s*:\s*'([^']*)'/
str = 'ticket:1 priority:5 delay:\'2019-08-31 02:53:27.720422\' delay:\'2019-08-30 00:04:10.681242\''

str.scan(re) do |match|
    puts match.to_s
end

Output 产量

["2019-08-31 02:53:27.720422"]
["2019-08-30 00:04:10.681242"]

If you wish to explore/simplify/modify the expression, it's been explained on the top right panel of regex101.com . 如果您希望探索/简化/修改表达式,请在regex101.com的右上方面板中进行说明 If you'd like, you can also watch in this link , how it would match against some sample inputs. 如果您愿意,您还可以在此链接中查看它与某些示例输入的匹配情况。


This is a suggestion for how you might extract all values of interest, not just the values for "delay" . 这是关于如何提取所有感兴趣的值的建议,而不仅仅是"delay"的值。 It permits any number of instances of "delay:'..." in the string. 它允许字符串中的任何数量的"delay:'..."实例。

str = "ticket:1 priority:5 delay:'2019-08-31 02:53:27.720422' delay:'2019-08-30 00:04:10.681242"

str.delete("'").
    split(/ +(?=ticket|priority|delay)/).
    each_with_object({}) do |s,h|
      key, value = s.split(':', 2)
      case key
      when 'delay'
        (h[key] ||= []) << value
      else
        h[key] = value
      end
    end
  #=> {"ticket"=>"1", "priority"=>"5",
  #    "delay"=>["2019-08-31 02:53:27.720422", "2019-08-30 00:04:10.681242"]}

The regular expression that is String#split 's argument reads, "match one or more spaces followed immediately by the string "ticket" , "priority" or "delay" , the expression 正则表达式是String#split的参数读取,“匹配一个或多个空格后紧跟字符串"ticket""priority""delay" ,表达式

(?=ticket|priority|delay)

being a positive lookahead . 是一个积极的前瞻

The steps are as follows. 步骤如下。

a = str.delete("'")
  #=> "ticket:1 priority:5 delay:2019-08-31 02:53:27.720422 delay:2019-08-30 00:04:10.681242"

b = a.split(/ +(?=ticket|priority|delay)/)
  #=> ["ticket:1", "priority:5", "delay:2019-08-31 02:53:27.720422",
  #    "delay:2019-08-30 00:04:10.681242"] 
c = b.each_with_object({}) do |s,h|
      key, value = s.split(':', 2)
      case key
      when 'delay'
        (h[key] ||= []) << value
      else
        h[key] = value
       end
     end
  #=> {"ticket"=>"1", "priority"=>"5",
  #    "delay"=>["2019-08-31 02:53:27.720422", "2019-08-30 00:04:10.681242"]}

Let's examine more closely the calculation of c . 让我们更仔细地研究c的计算。

enum = b.each_with_object({})
  #=> #<Enumerator: ["ticket:1", "priority:5", "delay:2019-08-31 02:53:27.720422",
  #      "delay:2019-08-30 00:04:10.681242"]:each_with_object({})>

The first value is generated by this enumerator and passed to the block, and the two block variables are assigned these values using array decompostion. 第一个值由此枚举器生成并传递给块,并使用数组分解为这两个块变量分配这些值。

 s, h = enum.next
   #=> ["ticket:1", {}] 
 s #=> "ticket:1" 
 h #=> {} 

The block calculation is then performed. 然后执行块计算。

key, value = s.split(':', 2)
  #=> ["ticket", "1"] 
key
  #=> "ticket" 
value
  #=> "1" 

case else applies, so case else适用,那么

h[key] = value
  #=> h["ticket"] = 1
h #=> {"ticket"=>"1"} 

The next element is generated by enum , the block variables are assigned values and block calculation is performed. 下一个元素由enum生成,块变量被赋值,并执行块计算。

s, h = enum.next
  #=> ["priority:5", {"ticket"=>"1"}] 
key, value = s.split(':', 2)
  #=> ["priority", "5"] 

case else again applies, so we execute case else再次适用,所以我们执行

h[key] = value
  #=> h["priority"] = "5" 
h #=> {"ticket"=>"1", "priority"=>"5"} 

Next, 下一个,

s, h = enum.next
  #=> ["delay:2019-08-31 02:53:27.720422", {"ticket"=>"1", "priority"=>"5"}] 
key, value = s.split(':', 2)
  #=> ["delay", "2019-08-31 02:53:27.720422"] 

case "delay" now applies, so we compute case "delay"现在适用,所以我们计算

(h[key] ||= []) << value
  #=> h[key] = (h[key] || []) << value
  #=> h["delay"] = (h["delay"] || []) << "2019-08-31 02:53:27.720422"
  #=> h["delay"] = (nil || []) << "2019-08-31 02:53:27.720422" 
  #=> h["delay"] = [] << "2019-08-31 02:53:27.720422
  #=> h["delay"] = ["2019-08-31 02:53:27.720422] 
h #=> {"ticket"=>"1", "priority"=>"5", "delay"=>["2019-08-31 02:53:27.720422"]}

Lastly, 最后,

s, h = enum.next
  #=> ["delay:2019-08-30 00:04:10.681242",
  #    {"ticket"=>"1", "priority"=>"5", "delay"=>["2019-08-31 02:53:27.720422"]}] 
key, value = s.split(':', 2)
  #=> ["delay", "2019-08-30 00:04:10.681242"] 
(h[key] ||= []) << value
  #=> ["2019-08-31 02:53:27.720422", "2019-08-30 00:04:10.681242"] 
h #=> {"ticket"=>"1", "priority"=>"5",
  #    "delay"=>["2019-08-31 02:53:27.720422", "2019-08-30 00:04:10.681242"]} 

In this last step, unlike the previous one, 在最后一步中,与前一步不同,

h[key] ||= []
  #=> ["2019-08-31 02:53:27.720422"] ||= []
  #=> ["2019-08-31 02:53:27.720422"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM