Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > [regexp] How to convert string "/regexp/i" to /regexp/i - ?

Reply
Thread Tools

[regexp] How to convert string "/regexp/i" to /regexp/i - ?

 
 
Joao Silva
Guest
Posts: n/a
 
      08-21-2009
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/.

 
Reply With Quote
 
 
 
 
Fleck Jean-Julien
Guest
Posts: n/a
 
      08-21-2009
2009/8/21 Joao Silva <>:
> 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

 
Reply With Quote
 
 
 
 
Justin Collins
Guest
Posts: n/a
 
      08-21-2009
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

 
Reply With Quote
 
Joao Silva
Guest
Posts: n/a
 
      08-21-2009
Fleck Jean-Julien wrote:
> 2009/8/21 Joao Silva <>:
>>
>> 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/.

 
Reply With Quote
 
Michal Zacik
Guest
Posts: n/a
 
      08-21-2009
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?
>



 
Reply With Quote
 
Justin Collins
Guest
Posts: n/a
 
      08-21-2009
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

 
Reply With Quote
 
Joao Silva
Guest
Posts: n/a
 
      08-21-2009
Michal Zacik wrote:
> Regexp.new(/regexp/i)


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

 
Reply With Quote
 
Pascal J. Bourguignon
Guest
Posts: n/a
 
      08-21-2009
Joao Silva <> writes:

> Fleck Jean-Julien wrote:
>> 2009/8/21 Joao Silva <>:
>>>
>>> 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__
 
Reply With Quote
 
Joao Silva
Guest
Posts: n/a
 
      08-21-2009
Pascal J. Bourguignon wrote:
> Joao Silva <> 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/.

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      08-21-2009
On Fri, Aug 21, 2009 at 12:00 PM, Pascal J.
Bourguignon<> wrote:
> Joao Silva <> writes:
>
>> Fleck Jean-Julien wrote:
>>> 2009/8/21 Joao Silva <>:
>>>>
>>>> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Convert formatted string to string? rockdale ASP .Net 3 04-12-2006 03:01 PM
convert string to raw string? Phd Python 3 12-06-2004 04:36 PM
Connection String object Convert to String Variable Type =?Utf-8?B?TWlrZSBNb29yZQ==?= ASP .Net 2 10-26-2004 02:43 PM
Convert string to a string array Andrew Banks ASP .Net 2 04-19-2004 04:45 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57