From: ahoward Date: 2002-11-15T02:20:33+09:00 Subject: Re: regex help On Thu, 14 Nov 2002, why the lucky stiff wrote: [snip] stiff> quote1_re = /((?:''|[^'])+)/ [snip] TESTING REGEX ((?:''|[^'])+) ' did not match - '' matched ('') ''' matched ('') '''' matched ('''') 'a matched (a) a' matched (a) ''a matched (''a) a'' matched (a'') '''a matched ('') a''' matched (a'') ''''a matched (''''a) a'''' matched (a'''') TESTING REGEX (?:^|[^'])('(?:[^']|'')*')(?:[^']|$) ' did not match - '' matched ('') ''' did not match - '''' matched ('''') 'a did not match - a' did not match - ''a matched ('') a'' matched ('') '''a did not match - a''' did not match - ''''a matched ('''') a'''' matched ('''') THE PROGRAM #!/usr/bin/env ruby regexs = [ %r{((?:''|[^'])+)}, %r{(?:^|[^'])('(?:[^']|'')*')(?:[^']|$)} ] strings = %w( ' '' ''' '''' 'a a' ''a a'' '''a a''' ''''a a'''' ) regexs.each do |r| puts "\nTESTING REGEX #{r.source}\n\n" strings.each do |s| m = r.match s format = "\t%-8.8s %-13.13s %8.8s\n" if m printf format, s, 'matched', "(#{m[1]})" else printf format, s, 'did not match', '-' end end end (?:^|[^'])('(?:[^']|'')*')(?:[^']|$) * beginning with the start of string or not a ' * followed by a ' * followed by zero or more of not ' or '' * followed by a ' * ending with the end of string or not a ' * the quoted string is captured, nothing else is -ara -- ==================================== | Ara Howard | NOAA Forecast Systems Laboratory | Information and Technology Services | Data Systems Group | R/FST 325 Broadway | Boulder, CO 80305-3328 | Email: ahoward@fsl.noaa.gov | Phone: 303-497-7238 | Fax: 303-497-7259 ====================================