Save regular expressions in ruby?

I am parsing a ruby ​​file to change the formatting of the data. I created a regular expression that has three matching groups that I want to temporarily store in variables. I am having problems so that matches are preserved, since it’s all the same zero.

Here is what I have not read so far.

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

Here are some examples of the data I use for testing.

"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"

Thanks,
Lf4

+3
source share
1 answer

This will not work:

match_array = line.scan(/regex/)

, " " , - regex. scan Regexp:

regex = Regexp.new('^"(\bhttps?://[-\w+&@#/%?=~_|$!:,.;]*[\w+&@#/%=~_|$])","(\w+|[\w._%+-]+@[\w.-]+\.[a-zA-Z]{2,4})","(\w{1,30})')
# ...
match_array = line.scan(regex)

, , CSV ( Ruby: 1.8.7 1.9) CSV , CSV. .

+5

All Articles