Ruby匹配多个字符串Regex

时间:2018-02-19 14:21:27

标签: ruby

我正在尝试增强下面的脚本,只打印那些不合适的唯一值 *)6个字符 - 字母数字和小写或 *)以map开头的单词,来自目录中的文件列表

我尝试了什么

values = []
@files = Dir.glob("*.txt")
for values in @files
 file = File.read(values)
 file.split(' ').each do |line|
    values.push(line.gsub(',', '')) if line.match(/[a-z0-9]{6}/) end or unless values.include? line.gsub(',', '') or line.match(/map_.*/)
  end
end

puts values

示例,

档案1

[id]
col1 = map_dr_check, map_iop, foo123
col2 = bar123, FOO123
col3 = ta2ngo, bar123

[/id_check]
@col2 = dr
@col1 = r

文件2

[id]
col1 = map_dr_check, map_iop, foo123
col2 = alp23r
col3 = poi90k, bar123

[/id_check]
@col2 = *
@col1 = r

预期输出

map_dr_check
map_iop
foo123
ta2ngo
bar123
alp23r
poi90k

但是我的输出是空的,我不确定我的正则表达式出错了,或者字符串是否支持.match方法。

1 个答案:

答案 0 :(得分:1)

使用Enumerable#grep

MainInstitution

对于您的示例(未经测试):

input = ... # get it from files, or whatever
input.split.grep(/\A[[:alnum:]]{6}\z|\Amap_.*/)