From: James Edward Gray II Date: 2007-01-14T13:26:06+09:00 Subject: Re: checking if a string matches a regexp - am I missing something? On Jan 13, 2007, at 10:09 PM, Rob Sanheim wrote: > On 1/13/07, James Edward Gray II wrote: >> On Jan 13, 2007, at 9:43 PM, Rob Sanheim wrote: >> >> > I figure that I must be missing something really obvious with this >> > question. But here goes: >> > >> > I want to know if a string matches a regular expression. I >> don't care >> > where the match begins, I don't want a matchdata object, I don't >> want >> > to check for nil, I just want a boolean indicating if a string >> matches >> > a regexp. And I want this to be one method call. From what I can >> > tell, this is the closest I can get: >> > >> > ("string" =~ reg) >= 0 # ugly >> >> Just drop the noise: >> >> "string" =~ reg # => int results are +true+ and nil is +false+ >> >> Hope that helps. >> > > True...but here is a cleaned up version of the place where I tried > to do that: > > def valid_query? > # query is a string accessor > if query_is_plain_text? > is_valid = valid_query_length? && (query =~ regex) > else > is_valid = query_is_url? > end > is_valid > end !!(query =~ regex) James Edward Gray II