[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

how to generate dynamic regexp pattern from strings?

Nanyang Zhan

1/9/2008 6:40:00 AM

Hi, Could anyone tell me how to generate regexp pattern from strings?

for example, how to code the return_pattern(str) method?
thanks!

str = "^test|production-(\\d+)"
return_pattern(str) ===> /^test|production-(\d+)/

def return_pattern(str)
?????????????????
end
--
Posted via http://www.ruby-....

3 Answers

ara.t.howard

1/9/2008 6:45:00 AM

0


On Jan 8, 2008, at 11:39 PM, Nanyang Zhan wrote:

> Hi, Could anyone tell me how to generate regexp pattern from strings?
>
> for example, how to code the return_pattern(str) method?
> thanks!
>
> str = "^test|production-(\\d+)"
> return_pattern(str) ===> /^test|production-(\d+)/
>
> def return_pattern(str)
> ?????????????????
> end
> --
> Posted via http://www.ruby-....
>

Regexp.escape string

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Nanyang Zhan

1/9/2008 6:57:00 AM

0

ara howard wrote:
> On Jan 8, 2008, at 11:39 PM, Nanyang Zhan wrote:
>
>> end
>> --
>> Posted via http://www.ruby-....
>>
>
> Regexp.escape string
>
> a @ http://codeforp...

Thank you!!
--
Posted via http://www.ruby-....

Eivind Eklund

1/9/2008 7:02:00 AM

0

On Jan 9, 2008 7:44 AM, ara howard <ara.t.howard@gmail.com> wrote:
> On Jan 8, 2008, at 11:39 PM, Nanyang Zhan wrote:
> > Hi, Could anyone tell me how to generate regexp pattern from strings?
> >
> > for example, how to code the return_pattern(str) method?
> > thanks!
> >
> > str = "^test|production-(\\d+)"
> > return_pattern(str) ===> /^test|production-(\d+)/
> >
> > def return_pattern(str)
> > ?????????????????
> > end
> >
>
> Regexp.escape string
>

Actually, Regexp.compile string or Regexp.new string

Regexp.escape avoids interpretation of special characters.

Eivind.