From: Zoltan Dezso Date: 2008-03-26T16:29:30+09:00 Subject: Re: sorting text in a file Adam Akhtar wrote: > Hi Ive been hacking away at this all morning and getting nowhere fast. > Im relatively new to ruby and im not so hot at regex. Hi, How about something like this quick script: don't forget the /m modifier for multiline matching mode. (it assumes that there is no newline in the artist name part though) File.open('events.txt', 'r') {|f| contents = f.read() contents.split(/\n\n\n\n/).each {|conevt| if (conevt =~ /([^\n]*)\n\n(.*)/im) artist = $1 description = $2 print "ARTIST: #{artist}\nDESC: #{description}\n\n" end } } > Second am i going about this the write way. Should I have just avoided > regex and simply read the file line by line using if structures to > figure out which lines are with which event??? From what I can see, I guess this is the format you have to deal with... in this case, I believe regexp are the way to go and you will only hurt yourself in the long term with switch-case spaghetti :) In case, you can get your hands on other formats, or if you are in charge of creating the data in the first place, I wouldn't recommend using plain text in the first place (yaml, xml, ini, whichever you like best), but I think that is not an option for you. Zaki -- Posted via http://www.ruby-forum.com/.