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 - ?

 
 
7stud --
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?


str = "/regexp/i"
pieces = str.split("/")
pattern = pieces[1]

my_regex = Regexp.new(pattern, true)

md = my_regex.match("hello ReGexP")
puts md[0]

--output:--
ReGexP


Or, more generally:

str = "/reg.*?exp/im"
pieces = str.split("/")
pattern = pieces[1]
flags = pieces[2]

arg_two = 0

flags.length.times do |i|
case flags[i, 1]
when "i"
arg_two |= Regexp::IGNORECASE
when "m"
arg_two |= Regexp::MULTILINE
when "x"
arg_two |= Regexp::EXTENDED
end

end

regex = Regexp.new(pattern, arg_two)

test_str =<<ENDOFSTRING
hello rEG
world exP
ENDOFSTRING

md = regex.match(test_str)
if md
puts md[0]
end

--output:--
rEG
world exP

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

 
Reply With Quote
 
 
 
 
Josh Cheek
Guest
Posts: n/a
 
      08-21-2009
[Note: parts of this message were removed to make it a legal post.]

class String
def to_r
Regexp.new(*strip.match(/\/(.*)\/(.*)/)[1,2])
end
end

"/regexp/i".to_r # => /regexp/i


On Fri, Aug 21, 2009 at 4:18 AM, Joao Silva <
http://www.velocityreviews.com/forums/(E-Mail Removed)> 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?
> --
> Posted via http://www.ruby-forum.com/.
>
>


 
Reply With Quote
 
 
 
 
7stud --
Guest
Posts: n/a
 
      08-21-2009
Josh Cheek wrote:
> class String
> def to_r
> Regexp.new(*strip.match(/\/(.*)\/(.*)/)[1,2])
> end
> end
>
> "/regexp/i".to_r # => /regexp/i
>


Doesn't work in this case:

str = "/reg.*?exp/m"

test_str =<<ENDOFSTRING
hello rEG
world exP
ENDOFSTRING

...nor when there is more than one flag.





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

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      08-21-2009
2009/8/21 Pascal J. Bourguignon <(E-Mail Removed)>:
> Joao Silva <(E-Mail Removed)> writes:
>
>> Fleck Jean-Julien wrote:
>>> 2009/8/21 Joao Silva <(E-Mail Removed)>:
>>>>
>>>> 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))


"Regexp.new" is superfluous:

irb(main):002:0> eval("/regexp/i").class
=> Regexp
irb(main):003:0>

Cheers

robert


--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
Josh Cheek
Guest
Posts: n/a
 
      08-21-2009
[Note: parts of this message were removed to make it a legal post.]

Sorry, it gave me what I expected, and Ruby is usually intuitive, so I
thought I got it right. This class, however, is not. I had to learn some of
it's internal logic to figure it out. (they define constants for each flag,
these constants are integers, you then have to add the values of the
constants of all the flags together to get the new options value)


class String
return nil unless self.strip.match(/\A\/(.*)\/(.*)\Z/mx)
regexp , flags = $1 , $2
return nil if !regexp || flags =~ /[^xim]/m

x = /x/.match(flags) && Regexp::EXTENDED
i = /i/.match(flags) && Regexp::IGNORECASE
m = /m/.match(flags) && Regexp::MULTILINE

Regexp.new regexp , [x,i,m].inject(0){|a,f| f ? a+f : a }
end
end

#fail cases
"regexp".to_r # => nil
"regexp/i".to_r # => nil
"/regexp/mk".to_r # => nil
<<-ENDOFSTRING.to_r # => nil
hello rEG
world exP
ENDOFSTRING
"/regexp/mk
hello rEG
world exP".to_r # => nil

#pass cases
"/reg/".to_r # => /reg/
" /reg/x ".to_r # => /reg/x
"/reg.*?exp/m".to_r # => /reg.*?exp/m
"/regexp/x".to_r # => /regexp/x
"/abc
def
ghi/xi".to_r # => /abc
# def
# ghi/ix

"//".to_r # => //
"/abc/i".to_r # => /abc/i
"/abc/x".to_r # => /abc/x
"/abc/m".to_r # => /abc/m
"/abc/ix".to_r # => /abc/ix
"/abc/im".to_r # => /abc/mi
"/abc/xm".to_r # => /abc/mx
"/abc/ixm".to_r # => /abc/mix
"/abc/mxi".to_r # => /abc/mix





On Fri, Aug 21, 2009 at 5:27 AM, 7stud -- <(E-Mail Removed)> wrote:

> Josh Cheek wrote:
> > class String
> > def to_r
> > Regexp.new(*strip.match(/\/(.*)\/(.*)/)[1,2])
> > end
> > end
> >
> > "/regexp/i".to_r # => /regexp/i
> >

>
> Doesn't work in this case:
>
> str = "/reg.*?exp/m"
>
> test_str =<<ENDOFSTRING
> hello rEG
> world exP
> ENDOFSTRING
>
> ...nor when there is more than one flag.
>
>
>
>
>
> --
> Posted via http://www.ruby-forum.com/.
>
>


 
Reply With Quote
 
Ben Giddings
Guest
Posts: n/a
 
      08-21-2009
On Aug 21, 2009, at 06:04, Joao Silva wrote:
> I tried this, but it's totally unsafe way (these strings are
> provided by
> user).


If the strings are provided by an untrusted source, don't use any
solution that uses eval().

Instead, use a solution that recognizes the string contains a regexp
and then uses a regexp constructor after pulling the relevant data out
of the string. Something along the lines of:

def parse_input(str)
if md = %r{^/(.*)+/(.*)*$}.match(str)
#it's a regexp
pattern = md[1]
if md[2]
# deal with the flags
flags = deal_with(md[2]
end
Regexp.new(pattern, flags)
else
...
end
end

Ben


 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      08-21-2009
Josh Cheek wrote:
> Sorry, it gave me what I expected, and Ruby is usually intuitive, so I
> thought I got it right.


Mine solution has problems too--for the same reason. It needs something
like this:

arg_two = nil if arg_two == 0
regex = Regexp.new(pattern, arg_two)
--
Posted via http://www.ruby-forum.com/.

 
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