From: Ryan Davis Date: 2011-08-11T07:33:05+09:00 Subject: Re: Odd regexp behavior On Aug 10, 2011, at 13:50 , Glen Holcomb wrote: > I'm running 1.9.2-p180 > > I have the following regexp: /\s(".*?")(\s|$)/ > > For some reason it isn't matching the end of the following line or any line > with a similar format. By end I mean the entire user-agent string > msnbot...htm)\"\n > > "207.46.13.53 - - [21/Apr/2010:04:05:29 -0600] \"GET > /dualcredit/courses/general.php HTTP/1.1\" 200 27731 \"-\" \"msnbot/2.0b (+ > http://search.msn.com/msnbot.htm)\"\n" > > However it matches against the following: > > " \"msnbot/2.0b (+http://search.msn.com/msnbot.htm)\"\n" > > I am at a total loss as to why. I'm not too sure how to go about debugging > it either. Don't match. You're not trying to match, you're trying to extract. I see two easy ways to do this: 1) split on /\"/ and get the field you want (the last one, not counting the newline). 2) scan for everything within quotes and get the last one. I'm sure there are others, but those are my two favorite go-to string methods. examples: s = "207.46.13.53 - - [21/Apr/2010:04:05:29 -0600] \"GET /dualcredit/courses/general.php HTTP/1.1\" 200 27731 \"-\" \"msnbot/2.0b (+http://search.msn.com/msnbot.htm)\"\n" p s.strip.split(/\"/).last # => "msnbot/2.0b (+http://search.msn.com/msnbot.htm)" p s.scan(/\".+?\"/).last[1..-2] # => "msnbot/2.0b (+http://search.msn.com/msnbot.htm)"