From: Glen Holcomb Date: 2011-08-12T22:28:02+09:00 Subject: Re: Odd regexp behavior --00163631081bde74ea04aa4ee1ca Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable On Thu, Aug 11, 2011 at 5:52 AM, Glen Holcomb wrote: > On Thu, Aug 11, 2011 at 12:41 AM, 7stud -- wrote= : > > > Also, > > > > > > str =3D "hello\nhello" > > > > str.scan(/^hell/) {|match| p match} > > puts "-" * 20 > > str.scan(/\Ahell/) {|match| p match} > > > > --output:-- > > "hell" > > "hell" > > -------------------- > > "hell" > > > > -- > > Posted via http://www.ruby-forum.com/. > > > > > Hmmm... maybe I should have posted from the beginning rather than where I > had gotten to in my attempt to solve my problem. > > I am tyring to parse log files and am pulling out encapsulated fields so > that I can split the line in a sane way. I have the following regex whic= h > I > am using to do that: > > /\s(".*?")(\s|$)/ > > Now as to why I'm looking for the \s before and the \s or $ after. It > turns > out that some of the user agent strings are in a format like "\"Custom > Agent\"=3D\"Mozilla ...\""\n > > I had been using the regex Ryan suggested earlier until I discovered the > nested quotes. > > The expression above works for quoted strings surrounded by spaces but no= t > the last one on the line. I've tried changing $ to \n and that didn't ma= ke > any difference. > > Here is the exact code I'm using: > > x =3D "encapsulatorhere" > stash =3D {} > > gen_encap_matches.each do |encapex| > line.gsub!(encapex) do |match| > x.next! > stash[x] =3D $1 > @job.log_format.separator + x + @job.log_format.separator > end > end > > gen_encap_matches just creates the regexes from a list of encapsulation > characters. > > I'm at a complete loss as to why it won't grab the last quoted string in > the > line. > > -- > "Hey brother Christian with your high and mighty errand, Your actions spe= ak > so loud, I can=92t hear a word you=92re saying." > > -Greg Graffin (Bad Religion) > So, after a little digging on Stackoverflow I decided to try an explicit lookahead. For what ever reason it works. /\s(".*?")(?=3D\s|$)/ matches where /\s(".*?")(\s|$)/ won't. --=20 "Hey brother Christian with your high and mighty errand, Your actions speak so loud, I can=92t hear a word you=92re saying." -Greg Graffin (Bad Religion) --00163631081bde74ea04aa4ee1ca--