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

Discussion in 'Ruby' started by Joao Silva, Aug 21, 2009.

  1. Joao Silva

    Joao Silva Guest

    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/.
     
    Joao Silva, Aug 21, 2009
    #1
    1. Advertising

  2. 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
     
    Fleck Jean-Julien, Aug 21, 2009
    #2
    1. Advertising

  3. 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
     
    Justin Collins, Aug 21, 2009
    #3
  4. Joao Silva

    Joao Silva Guest

    Re: How to convert string "/regexp/i" to /regexp/i - ?

    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/.
     
    Joao Silva, Aug 21, 2009
    #4
  5. Joao Silva

    Michal Zacik Guest

    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?
    >
     
    Michal Zacik, Aug 21, 2009
    #5
  6. 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
     
    Justin Collins, Aug 21, 2009
    #6
  7. Joao Silva

    Joao Silva Guest

    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/.
     
    Joao Silva, Aug 21, 2009
    #7
  8. Re: How to convert string "/regexp/i" to /regexp/i - ?

    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__
     
    Pascal J. Bourguignon, Aug 21, 2009
    #8
  9. Joao Silva

    Joao Silva Guest

    Re: How to convert string "/regexp/i" to /regexp/i - ?

    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/.
     
    Joao Silva, Aug 21, 2009
    #9
  10. Joao Silva

    Robert Dober Guest

    Re: How to convert string "/regexp/i" to /regexp/i - ?

    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
     
    Robert Dober, Aug 21, 2009
    #10
  11. Joao Silva

    7stud -- Guest

    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?


    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/.
     
    7stud --, Aug 21, 2009
    #11
  12. Joao Silva

    Josh Cheek Guest

    [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 <
    > 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/.
    >
    >
     
    Josh Cheek, Aug 21, 2009
    #12
  13. Joao Silva

    7stud -- Guest

    Re: How to convert string "/regexp/i" to /regexp/i - ?

    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/.
     
    7stud --, Aug 21, 2009
    #13
  14. Re: How to convert string "/regexp/i" to /regexp/i - ?

    2009/8/21 Pascal J. Bourguignon <>:
    > 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))


    "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/
     
    Robert Klemme, Aug 21, 2009
    #14
  15. Joao Silva

    Josh Cheek Guest

    Re: How to convert string "/regexp/i" to /regexp/i - ?

    [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 -- <> 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/.
    >
    >
     
    Josh Cheek, Aug 21, 2009
    #15
  16. Joao Silva

    Ben Giddings Guest

    Re: How to convert string "/regexp/i" to /regexp/i - ?

    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
     
    Ben Giddings, Aug 21, 2009
    #16
  17. Joao Silva

    7stud -- Guest

    Re: How to convert string "/regexp/i" to /regexp/i - ?

    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/.
     
    7stud --, Aug 21, 2009
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Banks

    Convert string to a string array

    Andrew Banks, Apr 19, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    58,084
    Andrew Banks
    Apr 19, 2004
  2. =?Utf-8?B?TWlrZSBNb29yZQ==?=

    Connection String object Convert to String Variable Type

    =?Utf-8?B?TWlrZSBNb29yZQ==?=, Oct 26, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,081
    Norman Yuan
    Oct 26, 2004
  3. rockdale

    Convert formatted string to string?

    rockdale, Apr 11, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    430
    rockdale
    Apr 12, 2006
  4. Mikel Lindsaar
    Replies:
    0
    Views:
    523
    Mikel Lindsaar
    Mar 31, 2008
  5. Replies:
    2
    Views:
    192
    John Bokma
    Aug 30, 2013
Loading...

Share This Page