我正在使用Ruby解析一个文件,以更改数据格式。我创建了一个正则表达式,其中有三个匹配组,我想将它们暂时存储在变量中。我遇到了问题,因为所有的匹配都是nil,无法存储。
以下是我从阅读中得到的内容:
regex = '^"(\bhttps?://[-\w+&@#/%?=~_|$!:,.;]*[\w+&@#/%=~_|$])","(\w+|[\w._%+-]+@[\w.-]+\.[a-zA-Z]{2,4})","(\w{1,30})'
begin
file = File.new("testfile.csv", "r")
while (line = file.gets)
puts line
match_array = line.scan(/regex/)
puts $&
end
file.close
end
这是一些我用于测试的样本数据。
"https://mail.google.com","Master","password1","","https://mail.google.com","",""
"https://login.sf.org","monster@gmail.com","password2","https://login.sf.org","","ctl00$ctl00$ctl00$body$body$wacCenterStage$standardLogin$tbxUsername","ctl00$ctl00$ctl00$body$body$wacCenterStage$standardLogin$tbxPassword"
"http://www.facebook.com","Beast","12345678","https://login.facebook.com","","email","pass"
"http://www.own3d.tv","Earth","passWOrd3","http://www.own3d.tv","","user_name","user_password"
谢谢您,
LF4