Forum: Ruby on Rails Regexp not matched

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cc1f82fad5e73398846d66c04c8df213?d=identicon&s=25 th8254 (Guest)
on 2007-06-27 23:58
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
Cc1f82fad5e73398846d66c04c8df213?d=identicon&s=25 th8254 (Guest)
on 2007-06-28 00:43
sorry, got it
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2007-07-02 00:04
(Received via mailing list)
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
  http://www.oreilly.com/catalog/9780596510657/
  "Test Driven Ajax (on Rails)"
  assert_xpath, assert_javascript, & assert_ajax
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-07-02 00:46
(Received via mailing list)
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

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
   RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
     & consulting:  Ruby Power and Light, LLC (http://www.rubypal.com)
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2007-07-02 02:33
(Received via mailing list)
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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-07-02 02:38
(Received via mailing list)
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

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
   RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
     & consulting:  Ruby Power and Light, LLC (http://www.rubypal.com)
This topic is locked and can not be replied to.