Regexp and using the matched value returned/found.

Discussion in 'Ruby' started by Jean Nibee, Apr 13, 2009.

  1. Jean Nibee

    Jean Nibee Guest

    Hi

    I'm trying to do a regex search and using what I find a key to a Hash to
    dig up some formatting info.

    But what's happening is the literal \\1 or \\0 (or \1 \0) is not being
    resolved to what's being found.

    Any help would be awesome.

    Ex: (Doesn't work)

    # regex looks like this: @swi_start = /SWI((dm)|(ph)|(ty)|(st))st/
    // executed in my code:
    line.gsub!( swi_start, self.colorize_token( '\\1' ) )
    # also not working: line.gsub!( swi_start, self.colorize_token( '\1'
    ) )

    def colorize_token( event )
    puts "Event: " << event
    return "<font color=\"#{@@color_list[ event ]}\"></font>"
    end

    Output:
    Event: \1

    Ex: (This worked!!!)

    line.gsub!( swi_start, "#{@red_start}\\1#{@red_end}") then

    Output:
    <font color="red">dm</font>

    Lastly please notes I've tried using " instead of ' in my method call
    without any change.
    --
    Posted via http://www.ruby-forum.com/.
    Jean Nibee, Apr 13, 2009
    #1
    1. Advertising

  2. Jean Nibee

    Heesob Park Guest

    Hi,

    2009/4/13 Jean Nibee <>:
    > Hi
    >
    > I'm trying to do a regex search and using what I find a key to a Hash to
    > dig up some formatting info.
    >
    > But what's happening is the literal \\1 or \\0 (or \1 \0) is not being
    > resolved to what's being found.
    >
    > Any help would be awesome.
    >
    > Ex: (Doesn't work)
    >
    > =C2=A0 # regex looks like this: @swi_start =3D /SWI((dm)|(ph)|(ty)|(st))s=

    t/
    > =C2=A0 // executed in my code:
    > =C2=A0 line.gsub!( swi_start, self.colorize_token( '\\1' ) )
    > =C2=A0 # also not working: line.gsub!( swi_start, self.colorize_token( '\=

    1'
    > ) )
    >
    > =C2=A0 def colorize_token( event )
    > =C2=A0 =C2=A0 =C2=A0puts "Event: " << event
    > =C2=A0 =C2=A0 =C2=A0return "<font color=3D\"#{@@color_list[ event ]}\"></=

    font>"
    > =C2=A0 end
    >
    > Output:
    > Event: \1
    >
    > Ex: (This worked!!!)
    >
    > =C2=A0line.gsub!( swi_start, "#{@red_start}\\1#{@red_end}") then
    >
    > Output:
    > <font color=3D"red">dm</font>
    >
    > Lastly please notes I've tried using " instead of ' in my method call
    > without any change.


    You should use block format in that case
    Try
    line.gsub!(swi_start){colorize_token($1)}
    or
    line.gsub!(swi_start){|x| colorize_token(x)}


    Regards,

    Park Heesob
    Heesob Park, Apr 13, 2009
    #2
    1. Advertising

  3. On 13.04.2009 05:56, Jean Nibee wrote:
    > I'm trying to do a regex search and using what I find a key to a Hash to
    > dig up some formatting info.
    >
    > But what's happening is the literal \\1 or \\0 (or \1 \0) is not being
    > resolved to what's being found.


    Heesob gave you the solution already but I'd like to add a bit of
    explanation why this is the solution.

    > Any help would be awesome.
    >
    > Ex: (Doesn't work)
    >
    > # regex looks like this: @swi_start = /SWI((dm)|(ph)|(ty)|(st))st/


    The capturing groups are not needed. This is sufficient:

    /SWI(dm|ph|ty|st)st/

    > // executed in my code:
    > line.gsub!( swi_start, self.colorize_token( '\\1' ) )


    You need to be aware that all method arguments are evaluated *before*
    the method is actually invoked. Since you need an argument for
    #colorize which depends on the current match this can *never* work
    because the #colorize is finished before #gsub! even starts matching.

    > # also not working: line.gsub!( swi_start, self.colorize_token( '\1'
    > ) )
    >
    > def colorize_token( event )
    > puts "Event: " << event
    > return "<font color=\"#{@@color_list[ event ]}\"></font>"
    > end
    >
    > Output:
    > Event: \1
    >
    > Ex: (This worked!!!)
    >
    > line.gsub!( swi_start, "#{@red_start}\\1#{@red_end}") then


    No, this does not do the same as you attempted to do with the code above
    because in this case the replacement cannot depend on the match. In
    other words, you always insert the same color.

    > Output:
    > <font color="red">dm</font>
    >
    > Lastly please notes I've tried using " instead of ' in my method call
    > without any change.


    This is irrelevant in this case as the quote type does not affect the
    time of evaluation (see above).

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    Robert Klemme, Apr 13, 2009
    #3
  4. Jean Nibee

    Jean Nibee Guest

    Excellent all

    Thank you for the info. Using the block based notation worked much
    better. (Also, Robert thank you for the additional info, very
    informative).

    My next question is:

    Can someone direct me to a link to reading materials (my Google skills
    seem to stink as I am not finding what I expect) on the pattern matching
    variables I can use like $1 mentioned above as I am now trying to match
    the WHOLE pattern and not just the "dm,ph,ty,st" portion.

    So when I use:

    line.gsub!(swi_start){colorize_token($1)}

    The $1 only returns the aforementioned 2 char strings, I'd like to get
    the whole thing "SWIdmst" or "SWItynd" etc etc.

    Thanks.
    --
    Posted via http://www.ruby-forum.com/.
    Jean Nibee, Apr 13, 2009
    #4
  5. Jean Nibee

    lasitha Guest

    On Mon, Apr 13, 2009 at 8:19 PM, Jean Nibee <> wrote:
    > [...]
    > Can someone direct me to a link to reading materials (my Google skills
    > seem to stink as I am not finding what I expect) on the pattern matching
    > variables I can use like $1 mentioned above as I am now trying to match
    > the WHOLE pattern and not just the "dm,ph,ty,st" portion.


    The keywords seem to be 'ruby pre-defined variables'...
    First two hits:
    http://is.gd/saBm
    http://is.gd/saDy

    > [...]
    > The $1 only returns the aforementioned 2 char strings, I'd like to get
    > the whole thing "SWIdmst" or "SWItynd" etc etc.


    $&

    solidarity,
    lasitha
    lasitha, Apr 13, 2009
    #5
    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. System Attendant
    Replies:
    0
    Views:
    282
    System Attendant
    Aug 28, 2003
  2. System Attendant
    Replies:
    0
    Views:
    314
    System Attendant
    Sep 2, 2003
  3. cirfu
    Replies:
    1
    Views:
    233
    Carl Banks
    Jun 23, 2008
  4. Joao Silva
    Replies:
    16
    Views:
    359
    7stud --
    Aug 21, 2009
  5. metfan
    Replies:
    6
    Views:
    108
    gnari
    Jan 12, 2004
Loading...

Share This Page