From: Shashank Date Date: 2002-11-15T12:22:11+09:00 Subject: Re: regex help Nice ! Learnt a lot from your examples ... Thanks ! "ahoward" wrote in message > 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 >