简体   繁体   English

如何通过正则表达式从 Ruby 中的字符串中提取年份

[英]How to extract the year via regex from a string in Ruby

I'm trying to extract the year from a string with this format:我正在尝试从具有以下格式的字符串中提取年份:

dataset_name = 'ALTVALLEDAOSTA000020191001.json'

I tried:我试过:

dataset_name[/<\b(19|20)\d{2}\b>/, 1]
/\b(19|20)\d{2}\b/.match(dataset_name)

I'm still reading the docs but so far I'm not able to achieve the result I want.我仍在阅读文档,但到目前为止我无法达到我想要的结果。 I'm really bad at regex.我真的很不擅长正则表达式。

Since your dataset name always ends in yyyymmdd.json , you can take a slice of the last 13-9 characters counting from the rear:由于您的数据集名称始终以yyyymmdd.json结尾,因此您可以从后面开始计算最后 13-9 个字符的一部分:

irb(main):001:0> dataset_name = 'ALTVALLEDAOSTA000020191001.json'
irb(main):002:0> dataset_name[-13...-9]
=> "2019"

You can also use a regex if you want a bit more precision:如果您想要更高的精度,也可以使用正则表达式:

irb(main):003:0> dataset_name =~ /(\d{4})\d{4}\.json$/
=> 18
irb(main):004:0> $1
=> "2019"

There are many ways to get to Rome.有很多方法可以到达罗马。

Starting with:从...开始:

foo = 'ALTVALLEDAOSTA000020191001.json'

Stripping the extended filename + extension to its basename then using a regex:将扩展文件名 + 扩展名剥离为其basename然后使用正则表达式:

ymd = /(\d{4})(\d{2})(\d{2})$/
ext = File.extname(foo)
File.basename(foo, ext) # => "ALTVALLEDAOSTA000020191001"

File.basename(foo, ext)[ymd, 1] # => "2019"
File.basename(foo, ext)[ymd, 2] # => "10"
File.basename(foo, ext)[ymd, 3] # => "01"

Using a regex against the entire filename to grab just the year:对整个文件名使用正则表达式来获取年份:

ymd = /^.*(\d{4})/
foo[ymd, 1] # => "1001"

or extracting the year, month and day:或提取年、月和日:

ymd = /^.*(\d{4})(\d{2})(\d{2})/
foo[ymd, 1] # => "2019"
foo[ymd, 2] # => "10"
foo[ymd, 3] # => "01"

Using String's unpack :使用字符串的unpack

ymd = '@18A4'
foo.unpack(ymd)  # => ["2019"]

or:或者:

ymd = '@18A4A2A2'
foo.unpack(ymd)  # => ["2019", "10", "01"]

If the strings are consistent length and format, then I'd work with unpack , because, if I remember right, it is the fastest, followed by String slicing, with anchored, then unanchored regular expressions trailing.如果字符串的长度和格式一致,那么我会使用unpack ,因为如果我没记错的话,它是最快的,然后是字符串切片,锚定,然后是非锚定的正则表达式尾随。

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

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