Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > regexp match and nil

Reply
Thread Tools

regexp match and nil

 
 
Adam Akhtar
Guest
Posts: n/a
 
      08-26-2008
heres my function

def remove_for_such_and_such(a_string)
if (%r{(.+)for.*}i =~ a_string)
match = a_string.match(%r{(.+)for.*}i)
a_string = match[1].strip
end
a_string
end

i want to know if that could be written better. I wrote the if clause
because sometimes a string may not match the regular expression. If i
remove the "if" clause it might lead to match returning nil which will
stop my program. Im wondring is there a way to avoid using if?? if so it
could help performance especially if this function was used with huge
arrays of strings.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      08-26-2008
On Tue, Aug 26, 2008 at 9:29 AM, Adam Akhtar <(E-Mail Removed)> wrote:
> heres my function
>
> def remove_for_such_and_such(a_string)
> if (%r{(.+)for.*}i =~ a_string)
> match = a_string.match(%r{(.+)for.*}i)
> a_string = match[1].strip
> end
> a_string
> end
>
> i want to know if that could be written better. I wrote the if clause
> because sometimes a string may not match the regular expression. If i
> remove the "if" clause it might lead to match returning nil which will
> stop my program. Im wondring is there a way to avoid using if?? if so it
> could help performance especially if this function was used with huge
> arrays of strings.


With your above code you are trying to match the regexp twice:
one for the =~ and one for the match, which is not optimal. I would
do it like this:

def remove_for_such_and_such(a_string)
m = a_string.match(%r{(.+)for.*}i)
a_string = m[1].strip if m
a_string
end

Anyway you need an if, because you need to know if the string matched or not.
If you want to avoid using if at all costs, you can play with rescue,
but I think
it's not as clear as checking explicitly. YMMV.

Jesus.

 
Reply With Quote
 
 
 
 
Adam Akhtar
Guest
Posts: n/a
 
      08-26-2008
thanks guys.. ill look into sub as well...couldnt tell what would happen
there if it didnt match but looks interesting.

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

 
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
["test string\n", nil] i need to strip \n and nil Bigmac Turdsplash Ruby 15 09-08-2009 09:19 AM
String#match vs. Regexp#match - confused Old Echo Ruby 1 09-04-2008 06:11 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
Integer(nil) versus Float(nil) versus String(nil) Christoffer Sawicki Ruby 5 09-02-2006 06:28 PM
RCR 303: nil should accept missing methods and return nil John Carter Ruby 64 05-19-2005 12:12 PM



Advertisments