From: "Einar Magnús Boson" Date: 2008-11-19T15:08:20+09:00 Subject: Re: ruby global regex question. On 19.11.2008, at 00:37 , Alan Johnson wrote: > On Tue, Nov 18, 2008 at 4:06 PM, knohr > wrote: > >> For the life of me, i can't figure out a ruby equivalent to perl's /g >> >> basically, i want to do the following >> >> >> while htmlSource=~m/(.*?)<\table>/g do >> tableSource=$1 >> tableSource=~m/Index (\d+)/ >> indexNumber=$1 >> >> while tableSource=~m/(.*?)<\/tr>/g do >> tableRowSource=$1 >> doSomethingWith(tableRowSource, indexNumber) >> end#while tableSource >> >> end#while htmlSource >> >> >> I will actually need to pull multiple vars, not just a single one, >> from the regex >> I will need to do the outer loop an unknown amount of times per >> document (0-20) and i will need to loop the inner an unknown amount >> of >> times (0-29) >> >> >> Thread safe would be a plus. >> >> >> any suggestions? >> >> > I think this does what you want, although I don't think gsub was > really made > for this purpose. > > def doSomethingWith(s) > print s, "\n" > end > > htmlSource = '
1,11,2
' > htmlSource << '2,11,2
' > > htmlSource.gsub(/(.*?)<\/table>/) do |t| > tableRowSource = $1 > tableRowSource.gsub(/(.*?)<\/tr>/) do |r| > doSomethingWith $1 > end > end > > -- > Alan That is pretty much how, except globals are hardly thread safe I think. Use scan instead of gsub: Here's something I wrote to extract information from data structured like this: - tablename + field1 + field2:string - table2name +field1 : string +field2 Table = Struct.new(:name, :fields) Field = Struct.new(:name, :type) def extract_db_spec(file) tables = [] doc = open(file, File::RDONLY) {|f|f.read} table_name = /\- (\w*)\s*?\n/ field_name = /(\s+\+ (\w+)\s*(\:\s*(\w*))?\n)/ doc.scan /#{table_name}(#{field_name}+)/ do |tablename, fields| t = Table.new tablename, [] fields.scan field_name do |junk, fieldname, junk2, type| if type.nil? || type == "" if /\w+_id/ === fieldname type = "int" else type = "string" end end t.fields << Field.new(fieldname, type) end tables << t end tables end einarmagnus