From: Robert Klemme Date: 2004-03-26T22:29:30+09:00 Subject: Re: [newbie] regexp - match quotation mark "gabriele renzi" schrieb im Newsbeitrag news:lfb860lpiprtl3drl90n19ron1303i9n2p@4ax.com... > il Fri, 26 Mar 2004 13:15:13 +0100, "Einar Buffer" > <_ebuffer_@hotmail.com> ha scritto:: > > >Hi, I'm trying to write a regular expression to match a quotation mark > >inside a string. Actually, what I want is to extract the text between two > >quotation marks, like this: > > > >This should be "simple", but I can't do it. > > > >I want to extract the word simple with a regular expression. > > > >Thanks a lot! > > should fit: > >> rgx=/"([^"]+)/ > => /"([^"]+)/ > >> m= rgx.match 'this is my "simple" sting' > => # > >> m[1] > => "simple" > > read like: > match a " and then read any charcater while it is not a " > > not that it does not handle escaped " This might help. robert module Scanner RX=/ "((?:[^"\\]|\\.)*)" | # double quoted strings '((?:[^'\\]|\\.)*)' | # single quoted strings (\S+) # others /ox def self.scan_non_ws(str, rx=RX) index = 0 tokens = block_given? ? nil : [] str.scan( rx ) do |m| token = m.detect{|x|not x.nil?} # puts "m=#{m.inspect}" # token = ( m[0]||m[1]||m[2] ) # p token if tokens tokens << token else yield token, index index += 1 end end tokens end end # module Scanner