Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > =~ and rescue

Reply
Thread Tools

=~ and rescue

 
 
Patrick Gundlach
Guest
Posts: n/a
 
      10-19-2003
Dear Ruby-Hackers,

i'd like to catch a malformed regular expression like this:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#!/usr/bin/ruby

begin
"abcd" =~ /*foo*/
rescue StandardError
puts "error"
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


The above does not work (-:4: invalid regular expression; there's no
previous pattern, to which '*' would define cardinality at 1: /*foo*/)

How do I catch this correctly?

Patrick
--
pg <at> levana .de
 
Reply With Quote
 
 
 
 
Jason Williams
Guest
Posts: n/a
 
      10-19-2003
In article <(E-Mail Removed)>, Patrick Gundlach wrote:
> Dear Ruby-Hackers,
>
> i'd like to catch a malformed regular expression like this:
> "abcd" =~ /*foo*/
> rescue StandardError
> puts "error"
> end


Why would you want to do that? Its an error that can be caught
at compile-time, so why let it go until run-time? You can fool
it into missing the error like this, if you want -

regexString = "*foo*"
begin
"abcd" =~ /#{regexString}/
rescue RegexpError
puts "Oops"
end

....but I don't see what this gains.

 
Reply With Quote
 
 
 
 
Patrick Gundlach
Guest
Posts: n/a
 
      10-19-2003
Hi,

> Why would you want to do that? Its an error that can be caught
> at compile-time, so why let it go until run-time?


Because I want to let a user create a regexp and use it in /../.


> You can fool it into missing the error like this, if you want -


[....]

great!

Thank you very much.

BTW: Is there something like a generic Error that can be used with
rescue?

Like

begin
something bad
rescue anyerror
recover
end

Patrick
 
Reply With Quote
 
Patrick Gundlach
Guest
Posts: n/a
 
      10-19-2003

ansering my own post...

> BTW: Is there something like a generic Error that can be used with
> rescue?


> Like
>
> begin
> something bad
> rescue anyerror
> recover
> end


I guess it is just rescue without any parameter?

Patrick
 
Reply With Quote
 
henon
Guest
Posts: n/a
 
      10-19-2003
Patrick Gundlach wrote:

[...]

>
> BTW: Is there something like a generic Error that can be used with
> rescue?


the superclass of all exceptions is Exception.

so:

begin
...
rescue Exception
end

will definitely catch any exceptions thrown between begin and end.

>
> Patrick


-- henon

 
Reply With Quote
 
henon
Guest
Posts: n/a
 
      10-19-2003
Patrick Gundlach wrote:
[...]

> I guess it is just rescue without any parameter?


No, that rescues just StandardError
>
> Patrick


also, if you get regexp input from your users be aware of the fact,
that a regexp may execute ruby code:

this is a regexp that prints Hello World!

/#{puts 'Hello World!'}/

cheers,
- henon

 
Reply With Quote
 
Patrick Gundlach
Guest
Posts: n/a
 
      10-19-2003
Hi,

henon <(E-Mail Removed)> writes:

> No, that rescues just StandardError


good to know. Next time I should read the pickaxe more carefully.

> also, if you get regexp input from your users be aware of the fact,
> that a regexp may execute ruby code:
>
> this is a regexp that prints Hello World!
>
> /#{puts 'Hello World!'}/


Oh no! Is there any simple way to circumvent this? Or do I have to
analyze (strip #{...} from) the regexp?

Thank you for the important hint.

Patrick
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      10-19-2003

"Patrick Gundlach" <(E-Mail Removed)> schrieb im
Newsbeitrag news:(E-Mail Removed)...
> Hi,
>
> > Why would you want to do that? Its an error that can be caught
> > at compile-time, so why let it go until run-time?

>
> Because I want to let a user create a regexp and use it in /../.


But then I'd use Regexp.new:

user_regexp = get_input()

begin
rx = Regexp.new( user_regexp, user_set_flags )
rescue RegexpError => e
user_feedback( e )
end

Regards

robert

 
Reply With Quote
 
Patrick Gundlach
Guest
Posts: n/a
 
      10-19-2003
Hello again,

henon <(E-Mail Removed)> writes:

> also, if you get regexp input from your users be aware of the fact,
> that a regexp may execute ruby code:
>
> this is a regexp that prints Hello World!
>
> /#{puts 'Hello World!'}/


But not in this case:

--------------------------------------------------
#!/usr/bin/ruby

malcode="#" + "{ puts 'hallo' }"

puts malcode # prints #{ puts 'hallo' }

"foo" =~ /#{malcode}/ # does not print anything
--------------------------------------------------

Am I safe using the last line?

Patrick
 
Reply With Quote
 
Patrick Gundlach
Guest
Posts: n/a
 
      10-19-2003
Hello Robert,


"Robert Klemme" <(E-Mail Removed)> writes:
> But then I'd use Regexp.new:
>
> user_regexp = get_input()
>
> begin
> rx = Regexp.new( user_regexp, user_set_flags )
> rescue RegexpError => e
> user_feedback( e )
> end


I now have

begin
do_something if mystring =~ user_regexp
rescue ....
user_feedback ...
end

what is the advantage of Regexp.new over my approach?

Patrick
--
You are your own rainbow!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Have you been blocked by firewall? Fresh web proxy comes to rescue.. fast reliable and sweet! James Computer Support 0 08-15-2007 09:14 AM
System Rescue CD and PartImage Ben Wylie Computer Support 7 03-31-2005 03:00 PM
Which are the absolute best programs to Repair and Rescue corrupted a unbootable Hard Drive? Arawak Computer Support 4 12-12-2003 10:35 PM
Image Rescue and Conon RAW files Jim Gizzi Digital Photography 2 10-08-2003 08:59 AM
TCPSocket and rescue Mark Probert Ruby 2 09-05-2003 02:10 PM



Advertisments