Regexp not matched


#1

Sorry, meant to post this in the rails forum. Anyways, how would I write
a rescue method to recover from a failed regexp parsing with error
“regexp not matched”

-Thanks


#2

sorry, got it


#3

th8254 wrote:

Sorry, meant to post this in the rails forum. Anyways, how would I write
a rescue method to recover from a failed regexp parsing with error
“regexp not matched”

Could you post some code? If your user puts a regexp into a string r
(and if
you trust that user not to taint your program’s interior), you just need
this:

if /#{ r }/ =~ sample rescue advise_user® && false


Phlip
http://www.oreilly.com/catalog/9780596510657/
“Test Driven Ajax (on Rails)”
assert_xpath, assert_javascript, & assert_ajax


#4

Hi –

On Sun, 1 Jul 2007, Phlip wrote:

if /#{ r }/ =~ sample rescue advise_user® && false

I don’t think (though if I’m wrong of course I’d like to find out)
that r can do any damage there. The evaluation of r is itself already
a string, so this:

r = “gets”
/#{r}/

does not do the same as:

/#{gets}/

The bigger issue here, though, is specifically rescuing from a regex
parsing error, which doesn’t seem to fall under the jurisdiction of
the usual runtime rescuing mechanism:

irb(main):029:0> if /[/; end rescue “Bad regex”
(irb):29: warning: regex literal in condition
SyntaxError: compile error
(irb):29: invalid regular expression; ‘[’ can’t be the last character
ie. can’t start range at the end of pattern: /[/

David


#5

Hi –

On Sun, 1 Jul 2007, Phlip wrote:

dblack wrote:

The bigger issue here, though, is specifically rescuing from a regex
parsing error, which doesn’t seem to fall under the jurisdiction of
the usual runtime rescuing mechanism:

rx = eval(’/’ + r + ‘/’) rescue whatever($!)

Also you can do:

Regexp.new® rescue…

David


#6

dblack wrote:

The bigger issue here, though, is specifically rescuing from a regex
parsing error, which doesn’t seem to fall under the jurisdiction of
the usual runtime rescuing mechanism:

rx = eval(’/’ + r + ‘/’) rescue whatever($!)


Phlip
http://www.oreilly.com/catalog/9780596510657/
“Test Driven Ajax (on Rails)”
assert_xpath, assert_javascript, & assert_ajax