From: Rick DeNatale Date: 2007-05-23T03:05:05+09:00 Subject: Re: Partial Regular Expression Matching On 5/22/07, Hans Fugal wrote: > Well that works for \w+ an \s+, but what if you want to match /01+0/? > You'd get a syntax error on 0111 even though it's a valid partial match. Han's I'm not sure I understand your use case. Perhaps you could provide some code as you would write it IF Ruby provided a match_partial method for Regexp. The normal use case for partial re matching is that you are processing interactively accumulated input, and want to check that what the user has typed in so far is a valid prefix for the valid inputs. As far as I can see the best way to do that with the current Ruby regexp engine is to write a regexp which fully matches all prefixes $ cat part_mat.rb full_pat = /^01+0/ part_pat = /^((0|01+)0?)?$/ (%w(0 01 010 0100 011 0110 01100) << "").each do |str| m = part_pat.match(str) if m puts "\"#{str}\" partially matches as \"#{m.string}\"" else puts "\"#{str}\" does not match" end end $ ruby part_mat.rb "0" partially matches as "0" "01" partially matches as "01" "010" partially matches as "010" "0100" does not match "011" partially matches as "011" "0110" partially matches as "0110" "01100" does not match "" partially matches as "" It might be possible to take a regexp and automatically generate another regexp which will match it's prefixes. Might make an interesting rubyquiz. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/