Robert Klemme
4/13/2009 11:43:00 AM
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