Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   [regexp] How to convert string "/regexp/i" to /regexp/i - ? (http://www.velocityreviews.com/forums/t859163-regexp-how-to-convert-string-regexp-i-to-regexp-i.html)

Joao Silva 08-21-2009 09:18 AM

[regexp] How to convert string "/regexp/i" to /regexp/i - ?
 
When i try to use:

>> Regexp.new("/regexp/i")

=> /\/regexp\/i/

But it's not what i want - i need:

/regexp/i

:-(

How i can convert this proper way?
--
Posted via http://www.ruby-forum.com/.


Fleck Jean-Julien 08-21-2009 09:28 AM

Re: [regexp] How to convert string "/regexp/i" to /regexp/i - ?
 
2009/8/21 Joao Silva <rubyforum@thisisnotmyrealemail.com>:
> When i try to use:
>
>>> Regexp.new("/regexp/i")

> =3D> /\/regexp\/i/
>
> But it's not what i want - i need:
>
> /regexp/i
>
> :-(
>
> How i can convert this proper way?


Try without the quotes

>> Regexp.new(/regexp/i)

=3D> /regexp/i

Cheers,

--=20
JJ Fleck
PCSI1 Lyc=E9e Kleber


Justin Collins 08-21-2009 09:34 AM

Re: [regexp] How to convert string "/regexp/i" to /regexp/i - ?
 
Joao Silva wrote:
> When i try to use:
>
>
>>> Regexp.new("/regexp/i")
>>>

> => /\/regexp\/i/
>
> But it's not what i want - i need:
>
> /regexp/i
>
> :-(
>
> How i can convert this proper way?
>


If that is exactly the kind of string you are expecting, I think you are
stuck with having to use eval.

irb(main):001:0> r = eval("/regexp/i")
=> /regexp/i
irb(main):002:0> r.match("REGEXP")
=> #<MatchData "REGEXP">

If you do not need the options, you could just grab the inner part and
make a regexp out of that:

irb(main):001:0> re = "/som.r.g.x\d+/i"
=> "/som.r.g.xd+/i"
irb(main):002:0> re = "/som.r.g.x\\d+/i".match(/\/(.*)\/[^\/]/)[1]
=> "som.r.g.x\\d+"
irb(main):003:0> /#{re}/.match("someregex1123")
=> #<MatchData "someregex1123">

Not really a great solution, though, as you lose information.

-Justin


Joao Silva 08-21-2009 09:36 AM

Re: How to convert string "/regexp/i" to /regexp/i - ?
 
Fleck Jean-Julien wrote:
> 2009/8/21 Joao Silva <rubyforum@thisisnotmyrealemail.com>:
>>
>> How i can convert this proper way?

>
> Try without the quotes
>
>>> Regexp.new(/regexp/i)

> => /regexp/i
>
> Cheers,


I cannot do this without quotes - i need get it from string. Only thing
i have is "/regexp/i" string.

Thanks!
--
Posted via http://www.ruby-forum.com/.


Michal Zacik 08-21-2009 09:36 AM

Re: [regexp] How to convert string "/regexp/i" to /regexp/i - ?
 
Regexp.new(/regexp/i)

Joao Silva wrote:
> When i try to use:
>
>
>>> Regexp.new("/regexp/i")
>>>

> => /\/regexp\/i/
>
> But it's not what i want - i need:
>
> /regexp/i
>
> :-(
>
> How i can convert this proper way?
>




Justin Collins 08-21-2009 09:49 AM

Re: [regexp] How to convert string "/regexp/i" to /regexp/i - ?
 
Justin Collins wrote:
> Joao Silva wrote:
>> When i try to use:
>>
>>
>>>> Regexp.new("/regexp/i")
>>>>

>> => /\/regexp\/i/
>>
>> But it's not what i want - i need:
>>
>> /regexp/i
>>
>> :-(
>>
>> How i can convert this proper way?
>>

>
> If that is exactly the kind of string you are expecting, I think you
> are stuck with having to use eval.
>
> irb(main):001:0> r = eval("/regexp/i")
> => /regexp/i
> irb(main):002:0> r.match("REGEXP")
> => #<MatchData "REGEXP">
>
> If you do not need the options, you could just grab the inner part and
> make a regexp out of that:
>
> irb(main):001:0> re = "/som.r.g.x\d+/i"
> => "/som.r.g.xd+/i"
> irb(main):002:0> re = "/som.r.g.x\\d+/i".match(/\/(.*)\/[^\/]/)[1]
> => "som.r.g.x\\d+"
> irb(main):003:0> /#{re}/.match("someregex1123")
> => #<MatchData "someregex1123">
>
> Not really a great solution, though, as you lose information.
>
> -Justin
>


I should add, for the second approach you could also parse the options
and then manually build up the regexp with the corresponding options.
That is the "non-lazy" way, though :)

-Justin


Joao Silva 08-21-2009 09:49 AM

Re: How to convert string "/regexp/i" to /regexp/i - ?
 
Michal Zacik wrote:
> Regexp.new(/regexp/i)


? Without ""?
--
Posted via http://www.ruby-forum.com/.


Pascal J. Bourguignon 08-21-2009 09:58 AM

Re: How to convert string "/regexp/i" to /regexp/i - ?
 
Joao Silva <rubyforum@thisisnotmyrealemail.com> writes:

> Fleck Jean-Julien wrote:
>> 2009/8/21 Joao Silva <rubyforum@thisisnotmyrealemail.com>:
>>>
>>> How i can convert this proper way?

>>
>> Try without the quotes
>>
>>>> Regexp.new(/regexp/i)

>> => /regexp/i
>>
>> Cheers,

>
> I cannot do this without quotes - i need get it from string. Only thing
> i have is "/regexp/i" string.


string="/regexp/i"
Regexp.new(eval(string))


--
__Pascal Bourguignon__

Joao Silva 08-21-2009 10:04 AM

Re: How to convert string "/regexp/i" to /regexp/i - ?
 
Pascal J. Bourguignon wrote:
> Joao Silva <rubyforum@thisisnotmyrealemail.com> writes:
>
>>> Cheers,

>>
>> I cannot do this without quotes - i need get it from string. Only thing
>> i have is "/regexp/i" string.

>
> string="/regexp/i"
> Regexp.new(eval(string))


I tried this, but it's totally unsafe way (these strings are provided by
user).
--
Posted via http://www.ruby-forum.com/.


Robert Dober 08-21-2009 10:07 AM

Re: How to convert string "/regexp/i" to /regexp/i - ?
 
On Fri, Aug 21, 2009 at 12:00 PM, Pascal J.
Bourguignon<pjb@informatimago.com> wrote:
> Joao Silva <rubyforum@thisisnotmyrealemail.com> writes:
>
>> Fleck Jean-Julien wrote:
>>> 2009/8/21 Joao Silva <rubyforum@thisisnotmyrealemail.com>:
>>>>
>>>> How i can convert this proper way?
>>>
>>> Try without the quotes
>>>
>>>>> Regexp.new(/regexp/i)
>>> =3D> /regexp/i
>>>
>>> Cheers,

>>
>> I cannot do this without quotes - i need get it from string. Only thing
>> i have is "/regexp/i" string.

>
> string=3D"/regexp/i"
> Regexp.new(eval(string))
>
>
> --
> __Pascal Bourguignon__
>

Eval is a mighty tool for a tiny task

option =3D str[-1] # Ruby 1.9, use [-1,1] in 1.8

Regexp::new( str[1..-3], option )

HTH
R


--=20
module Kernel
alias_method :=EB, :lambda
end



All times are GMT. The time now is 03:17 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.