[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Regexp rescue method

th8254

6/27/2007 6:41:00 PM

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

-Thanks

--
Posted via http://www.ruby-....

3 Answers

Tim Hunter

6/27/2007 8:06:00 PM

0

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.

--
RMagick OS X Installer [http://rubyforge.org/project...]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?for...]
RMagick Installation FAQ [http://rmagick.rubyforge.org/instal...]


James Gray

6/27/2007 8:49:00 PM

0

On Jun 27, 2007, at 3:05 PM, Tim Hunter 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 Gray II


Florian Aßmann

6/27/2007 9:50:00 PM

0

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 Gray II schrieb:
> On Jun 27, 2007, at 3:05 PM, Tim Hunter 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 Gray II
>
>
>