From: William James Date: 2007-09-19T00:15:08+09:00 Subject: Re: finding string matches, in order, in a file On Sep 18, 8:28 am, Peter Bailey wrote: > William James wrote: > > Peter Bailey wrote: > >> Hi, > >> I've got files I want to parse. I'm using a string scan routine that > >> populates an array. I need to pull the entries of that array out, in > >> order, eventually. I'm getting an array all right, but, I don't > >> understand its order. The first instance in the string, meaning the > >> whole file, is way down the list in the array. The first entry in the > >> array is an entry that's 300 lines deep into the file. Why isn't the > >> first instance in the string, the file, the first entry in the array? > > > The file may have multiple copies of some entries, and > > your regexp may be botched. > > >> xmlfile.scan(/\n(.*)<\/issue>\n?/) > > > I don't like the looks of that regular expression. Try this one. > > > /\n(.*?)<\/issue>\n?/m > > Thanks, William. I tried your regex, but, I'm still getting the first > entry as one that's 300 lines deep into the file. In fact, the results > look exactly the same to me. Don't give up yet. A regular expression is a very concentrated piece of code, and it very often requires tweeking. Can you show us the first entry in the file that should be matched? That would enable us to test our reg.exps. Some tricky points. A . won't match a newline unless the m modifier is at the end of the regexp. .* will often match too much unless you make it non-greedy by appending ? (i.e., .*?). Sometimes it's best to make the regexp case-insensitive by using the i modifier. You may assume that your text will always have I'm Issue XIV, who are you? I'm Issue XX, are you? }.scan( /\s*(.*?)<\/issue>/m){ p $1 }