有没有办法让这个工作正确的情况下?
- field = "head_count_2011_10_75"
- case field
- when match(/head_count_\d{4}_\d{1,2}_\d{1,4}/i)
- puts "regex 1"
- when match(/dmi_\d{4}_\d{1,4}/i)
- puts "regex 2
- end
我知道如果符合以下条件我可以做到:
- if field.match(/head_count_\d{4}_\d{1,4}/i)
- puts "regex 1"
- elsif field.match(/dmi_\d{4}_\d{1,4}/i)
- puts "regex 2"
- end
只是寻找更清洁的解决方案.
解决方法
只需删除匹配:
- field = "head_count_2011_10_75"
- case field
- when /head_count_\d{4}_\d{1,4}/i
- puts "regex 1"
- when /dmi_\d{4}_\d{1,4}/i
- puts "regex 2
- end