# Regexp not matched

**URL:** <https://www.ruby-forum.com/t/regexp-not-matched/107978>\
**Category:** Rails\
**Created:** [June 27, 2007, 11:58pm UTC](https://www.ruby-forum.com/t/regexp-not-matched/107978 "2007-06-27T23:58:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![th8254](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/59ef9b/32.png) [@th8254](https://www.ruby-forum.com/u/th8254)\
**Post date:** [June 27, 2007, 11:58pm UTC](https://www.ruby-forum.com/t/regexp-not-matched/107978/1 "2007-06-27T23:58:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![th8254](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/59ef9b/32.png) [@th8254](https://www.ruby-forum.com/u/th8254)\
**Post date:** [June 28, 2007, 12:43am UTC](https://www.ruby-forum.com/t/regexp-not-matched/107978/2 "2007-06-28T00:43:34Z")

</div>

sorry, got it

---

<div class="post-metadata">

**Author:** ![th8254](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/59ef9b/32.png) [@th8254](https://www.ruby-forum.com/u/th8254)\
**Post date:** [July 2, 2007, 12:04am UTC](https://www.ruby-forum.com/t/regexp-not-matched/107978/3 "2007-07-02T00:04:23Z")

</div>

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]](http://www.oreilly.com/catalog/9780596510657/)  
“Test Driven Ajax (on Rails)”  
assert\_xpath, assert\_javascript, & assert\_ajax

---

<div class="post-metadata">

**Author:** ![th8254](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/59ef9b/32.png) [@th8254](https://www.ruby-forum.com/u/th8254)\
**Post date:** [July 2, 2007, 12:46am UTC](https://www.ruby-forum.com/t/regexp-not-matched/107978/4 "2007-07-02T00:46:26Z")

</div>

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](http://www.awprofessional.com/title/0321509242))  
RUBY FOR RAILS ([Ruby for Rails](http://www.manning.com/black))
- Ruby/Rails training  
& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![th8254](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/59ef9b/32.png) [@th8254](https://www.ruby-forum.com/u/th8254)\
**Post date:** [July 2, 2007, 2:38am UTC](https://www.ruby-forum.com/t/regexp-not-matched/107978/5 "2007-07-02T02:38:34Z")

</div>

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](http://www.awprofessional.com/title/0321509242))  
RUBY FOR RAILS ([Ruby for Rails](http://www.manning.com/black))
- Ruby/Rails training  
& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![th8254](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/59ef9b/32.png) [@th8254](https://www.ruby-forum.com/u/th8254)\
**Post date:** [July 2, 2007, 2:33am UTC](https://www.ruby-forum.com/t/regexp-not-matched/107978/6 "2007-07-02T02:33:34Z")

</div>

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]](http://www.oreilly.com/catalog/9780596510657/)  
“Test Driven Ajax (on Rails)”  
assert\_xpath, assert\_javascript, & assert\_ajax
