我需要开发一种方法,从字符串中提取数据库中记录的含义。以下是 a 字符串的示例:
MyString = "Purse $75,000. (up To $14,250 Nysbfoa) For Maidens, Fillies And Mares Three Years Old And Upward. Three Year Olds, 118 Lbs.; Older, 123 Lbs. One And One Eighth Miles. (Inner turf)"
给定字符串,我需要以可以创建race_record的方式处理它:
race_record[:purse] = 75000
race_record[:race_type] = "Maidens"
race_record[:sex] = "Fillies And Mares"
race_record[:age] = "Three Year Old And Upward"
race_record[:distance] = "One And One Eighth Miles"
race_record[:surface] = "inner turf"
我打算使用 ruby 和一系列正则表达式来提取数据。例如:
race_record[:purse] = Mystring.scan(/(?<=\Purse\s[$])(.*?)(?=\.)/)
race_record[:race_type] = Mystring.sub(....)
etc.
我的问题不是正确的正则表达式是什么。鉴于目标,我提出的方法是正确的方法,还是有更好的方法,甚至是可以完成繁重工作的宝石?