Topic: Ruby Reg Ex

I have a regular expression that uses a variable with in it to match, however the variable can contain reserved characters (dot slashes and the like).  How do I tell the regex engine to interpret those vars as raw text?

def print_report_with_errors(report)
  report_text = report.report
  for error in report.errors
    if error.relevant_text.length > 0
      report_text = report_text.gsub(/#{error.relevant_text}/,
      "<span id='error_intext#{error.id}'class='error_type#{error.error_type_id}'><strong>#{error.explination}: </strong>#{error.relevant_text}</span>")
    end
  end
  return report_text
end

If art interprets our dreams, the computer executes them in the guise of programs.

-SICP (Abelson, Sussman)

Re: Ruby Reg Ex

Don't use regexp wink

 report_text = report_text.gsub(error.relevant_text,
       "<span id='error_intext#{error.id}'class='error_type#{error.error_type_id}'><strong>#{error.explination}: </strong>#{error.relevant_text}</span>")

Re: Ruby Reg Ex

Sounded great to me, but it still doesn't work.  If my variable contains a period it doesn't find the pattern...

def print_report_with_errors(report)
  report_text = report.report
  for error in report.errors
    if error.relevant_text.length > 0
      report_text = report_text.gsub(error.relevant_text,"<span id='error_intext#{error.id}' class='error_type#{error.error_type_id}'><strong>#{error.explination}: </strong>#{error.relevant_text}</span>")
    end
  end
  return report_text
end

Any ideas?

If art interprets our dreams, the computer executes them in the guise of programs.

-SICP (Abelson, Sussman)

Re: Ruby Reg Ex

Hrm, the Rdoc specifically states "if it is a String then no regular expression metacharacters will be interpreted".  Not only that but a period will match a single character, so it should match a period.  There must be something else going on.

Re: Ruby Reg Ex

In case anyone wants to know the answer to the original question, you can include a literal string variable into a regular expression by escaping it using Regexp.escape

But, Ben is correct. In this case you shouldn't use a regular expression at all because you aren't using any regular expression metacharacters.

Railscasts - Free Ruby on Rails Screencasts

Re: Ruby Reg Ex

I need help... with ruby this time.  It seems that if I pass match a string with the same value as the variable I want to pass that it returns a match object.

@report.report.match("The NG tube is within the stomach. The left upper extremity PICC is in stable")

But when I pass in a variable it returns nil
@report.report.match(@error.relevant_text)
@report.report.match("#{@error.relevant_text}")
@report.report.match(Regexp.escape(@error.relevant_text))

All three return nil.  This only occurs when there is a . (period) in the string. I did this in the console.  Any ideas?

Last edited by circuitshaman (2006-08-31 15:13:04)

If art interprets our dreams, the computer executes them in the guise of programs.

-SICP (Abelson, Sussman)

Re: Ruby Reg Ex

What does this return?

@error.relevant_text == "The NG tube is within the stomach. The left upper extremity PICC is in stable"

Last edited by ryanb (2006-08-31 16:04:32)

Railscasts - Free Ruby on Rails Screencasts

Re: Ruby Reg Ex

It returns true.

If art interprets our dreams, the computer executes them in the guise of programs.

-SICP (Abelson, Sussman)

Re: Ruby Reg Ex

That is certainly very strange. I know this works:

var1 = 'Hello World'
var2 = 'Hello'
var1.match(var2)
# => #<MatchData:0x352094>

So I would go from there and slowly replicate what you are doing with report and error until it breaks.

Railscasts - Free Ruby on Rails Screencasts

Re: Ruby Reg Ex

With a clear head from a nice long weekend I found the problem.  The relevant text string is a copy and paste from the webpage and since html cares little for whitespace whenever there was a double space between sentences the error string wouldn't match.  Shame on me for thinking it was a regex problem... shame.  Thanks for all your help though.  To all those debugging something right now stop it!  Work on something else and the problems will always come to you later.

Thanks again.

If art interprets our dreams, the computer executes them in the guise of programs.

-SICP (Abelson, Sussman)

Re: Ruby Reg Ex

circuitshaman wrote:

To all those debugging something right now stop it!  Work on something else and the problems will always come to you later.

Good advice. You can also try Rubber Ducking. Describing the problem to a rubber duck always helps. smile

Railscasts - Free Ruby on Rails Screencasts

Re: Ruby Reg Ex

hehe, I don't know how many times I'm started explaining a problem to my wife, who is not a programmer, and in doing so I figure out the solution.