Regexp not matched

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

sorry, got it

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(r) && false


Phlip
Test Driven Ajax (on Rails) [Book]
“Test Driven Ajax (on Rails)”
assert_xpath, assert_javascript, & assert_ajax

Hi –

On Sun, 1 Jul 2007, Phlip wrote:

if /#{ r }/ =~ sample rescue advise_user(r) && 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

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(r) rescue…

David

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
Test Driven Ajax (on Rails) [Book]
“Test Driven Ajax (on Rails)”
assert_xpath, assert_javascript, & assert_ajax