简体   繁体   English

Ruby Regex:在模式后返回多个匹配项

[英]Ruby Regex: Return multiple matches after a pattern

I've googled for a good bit now and I can't figure out this regex on my own. 我现在用谷歌搜索了一下,我不能自己弄清楚这个正则表达式。 I'd like to pick up all the days of the week that occur between the 'Validation day:' and 'all_exception_rules' text: 我想了解“验证日:”和“all_exception_rules”文本之间发生的一周中的所有日子:

String to search: 要搜索的字符串:

--- !ruby/object:IceCube::Schedule start_time: 2012-04-28 13:38:49.334561000 -07:00 end_time: duration: all_recurrence_rules: - !ruby/object:IceCube::WeeklyRule validations: :interval: - !ruby/object:IceCube::Validations::WeeklyInterval::Validation interval: 1 week_start: :sunday :base_hour: - !ruby/object:IceCube::Validations::ScheduleLock::Validation type: :hour :base_min: - !ruby/object:IceCube::Validations::ScheduleLock::Validation type: :min :base_sec: - !ruby/object:IceCube::Validations::ScheduleLock::Validation type: :sec :day: - !ruby/object:IceCube::Validations::Day::Validation day: - monday - tuesday - wednesday - thursday - friday all_exception_rules: [] ---!ruby / object:IceCube :: Schedule start_time:2012-04-28 13:38:49.334561000 -07:00 end_time:duration:all_recurrence_rules: - !ruby / object:IceCube :: WeeklyRule验证:: interval: - !ruby / object:IceCube :: Validations :: WeeklyInterval ::验证间隔:1 week_start :: sunday:base_hour: - !ruby / object:IceCube :: Validations :: ScheduleLock ::验证类型:: hour:base_min: - ! ruby / object:IceCube :: Validations :: ScheduleLock ::验证类型:: min:base_sec: - !ruby / object:IceCube :: Validations :: ScheduleLock ::验证类型:: sec:day: - !ruby / object: IceCube :: Validations :: Day :: Validation day: - 星期一 - 星期二 - 星期三 - 星期四 - 星期五 all_exception_rules:[]

The closest I could get on rubular was: /Validation day: - (.*) all_exception/ . 我最接近rubular的是: /Validation day: - (.*) all_exception/ This picks up all days (plus whitespace, and dashes) in rubular, but is returning Nil in my rails app. 这会在rubular中获取所有日子(加上空格和破折号),但在我的rails应用程序中返回Nil

  • Any idea why this would work on rubular, but not my app? 知道为什么这会对rubular有效,但不是我的应用程序吗?

  • Is there an easy way to pick up an array of the days without the whitespace and dashes? 有没有一个简单的方法来获取没有空格和破折号的日子?

Does this help? 这有帮助吗?

s.scan(/-\s(\w+)\s/) 
#=> [["monday"], ["tuesday"], ["wednesday"], ["thursday"], ["friday"]]

Or: 要么:

s.scan(/-\s(\w+)\s/).map(&:first).join(" ") 
#=> "monday tuesday wednesday thursday friday"

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

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