# Regexp rescue method

**URL:** <https://www.ruby-forum.com/t/regexp-rescue-method/107956>\
**Category:** Ruby\
**Created:** [June 27, 2007, 8:41pm UTC](https://www.ruby-forum.com/t/regexp-rescue-method/107956 "2007-06-27T20:41:07Z")\
**Posts on this page:** 4\
**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, 8:41pm UTC](https://www.ruby-forum.com/t/regexp-rescue-method/107956/1 "2007-06-27T20:41:07Z")

</div>

How would I write a rescue method to recover from a failed regexp  
parsing?

-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 27, 2007, 10:06pm UTC](https://www.ruby-forum.com/t/regexp-rescue-method/107956/2 "2007-06-27T22:06:52Z")

</div>

th8254 wrote:

> How would I write a rescue method to recover from a failed regexp  
> parsing?
> 
> -Thanks

I don’t think that raises an error. You’d have to check for a nil result  
instead.

---

<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, 10:49pm UTC](https://www.ruby-forum.com/t/regexp-rescue-method/107956/3 "2007-06-27T22:49:57Z")

</div>

On Jun 27, 2007, at 3:05 PM, Tim H. wrote:

> th8254 wrote:
> 
> > How would I write a rescue method to recover from a failed regexp  
> > parsing?
> > 
> > -Thanks
> 
> I don’t think that raises an error. You’d have to check for a nil  
> result instead.

I took the question to mean, how would I recover from a malformed  
Regexp error like:

> > re = /[/  
> > SyntaxError: compile error  
> > (irb):1: invalid regular expression; ‘[’ can’t be the last character  
> > ie. can’t start range at the end of pattern: /[/  
> > from (irb):1

I don’t know of a way to recover from that specific example either,  
which is why I didn’t jump to answer:

> > begin  
> > ?\> re = /[/
> 
> > rescue SyntaxError  
> > puts “Your Regexp is malformed.”  
> > end  
> > SyntaxError: compile error  
> > (irb):3: invalid regular expression; ‘[’ can’t be the last character  
> > ie. can’t start range at the end of pattern: /[/  
> > from (irb):6

But, if you change the way the expression is built, it does seem to  
become a recoverable error:

> > begin  
> > ?\> re = Regexp.new("[")
> 
> > rescue RegexpError  
> > puts “Your Regexp is malformed.”  
> > end  
> > Your Regexp is malformed.  
> > =\> nil

James Edward G. II

---

<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:51pm UTC](https://www.ruby-forum.com/t/regexp-rescue-method/107956/4 "2007-06-27T23:51:02Z")

</div>

I can’t catch it even with rescue Object… wow.

you could also eval it, k, thats even more evil:

begin  
re = eval ‘/abc/’  
p re  
re = eval ‘/[/’  
rescue SyntaxError  
puts ‘hit’  
end

Sincerely  
Florian

James Edward G. II schrieb:
