[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Does ruby support global expression?

Jarod Zhu

1/12/2009 1:57:00 PM

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?

Thanks
3 Answers

Robert Dober

1/12/2009 2:33:00 PM

0

On Mon, Jan 12, 2009 at 2:59 PM, Jarod Zhu <zhujie2003info@gmail.com> 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.

Robert Klemme

1/12/2009 7:40:00 PM

0

On 12.01.2009 15:33, Robert Dober wrote:
> On Mon, Jan 12, 2009 at 2:59 PM, Jarod Zhu <zhujie2003info@gmail.com> 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."

Small remark: I'd split this up, i.e.

class String
def to_glob
Regexp::new(
scan( /(.*?)\*([^*]*)/ ).
inject("") { | r , ( prefix, suffix ) |
r << prefix.escape << '\w+' << suffix.escape }
)
end
end

This makes for more efficient matching if the pattern is to be applied
to multiple strings.

Here's how I'd do it:

class String
def to_glob
Regexp.new('\\A' << gsub(/\\([^*?])/, '\\1').split(/(\\?[*?])/).map
do |s|
case s
when '*': '.*'
when '?': '.'
when '\\*', '\\?': s
else Regexp.escape(s)
end
end.join << '\\z')
end
end

gl = "foo*b\\ar\\*a".to_glob
p gl

%w{foo foobar*a foo123bar foobar*a}.each do |s|
p [s, gl =~ s]
end

Kind regards

robert


--
remember.guy do |as, often| as.you_can - without end

Robert Dober

1/12/2009 7:58:00 PM

0

On Mon, Jan 12, 2009 at 8:39 PM, Robert Klemme
<shortcutter@googlemail.com> wrote:

> Small remark: I'd split this up, i.e.
Spoiling all the fun for the profilers ;)
Good point though.
Robert