Regexp not matched

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

Hi --

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

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

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($!)

Hi --