From: Robert Dober Date: 2009-01-12T23:33:10+09:00 Subject: Re: Does ruby support global expression? On Mon, Jan 12, 2009 at 2:59 PM, Jarod Zhu wrote: > Hi, all > > In my project, there are some rules using global expression( '*' > as one or more characters ) to match some functions' output. > Such as: > rule1: the * apple (will match the output "the red apple") > rule2: the * + * game (will match the output "the wonderful + > successful game") > > I want to use ruby to realize the match check, but I only know > ruby supports regular expression. > So I have to change rules to the form of regular expressions( "*" => ". > +" and add '\' to other regular expession keywords like '+', '.' etc.) > > Is there any way to use global expression directly in ruby? No, better make it ourselves class String def escape return "" if empty? Regexp::escape self end def glob? a_string Regexp::new( scan( /(.*?)\*([^*]*)/ ). inject("") { | r , ( prefix, suffix ) | r << prefix.escape << '\w+' << suffix.escape } ).match( a_string ) end end p "ab *c*d*".glob?( "ab andcordequally" ) p "a*b".glob?( "ab" ) p "etc.etc." HTH R.