checking if a string matches a regexp - am I missing something?

Discussion in 'Ruby' started by Rob Sanheim, Jan 14, 2007.

  1. Rob Sanheim

    Rob Sanheim Guest

    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:

    reg = /\w/
    reg === "string" # close, but I want to send the message to the
    string, not the other way around
    ("string" =~ reg) >= 0 # ugly
    ("string" =~ reg) != 0 # still ugly
    "string".include? reg # this would seem to follow the ruby way, but it
    throws a TypeError

    If I'm not missing some other trick to do this, why doesn't include?
    just allow regexp's like so many of the other (slice, index, scan,
    etc) string methods?

    - Rob
    Rob Sanheim, Jan 14, 2007
    #1
    1. Advertising

  2. 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.

    James Edward Gray II
    James Edward Gray II, Jan 14, 2007
    #2
    1. Advertising

  3. Rob Sanheim

    Guest

    On Sun, 14 Jan 2007, Rob Sanheim wrote:

    > 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:
    >
    > reg = /\w/
    > reg === "string" # close, but I want to send the message to the
    > string, not the other way around
    > ("string" =~ reg) >= 0 # ugly
    > ("string" =~ reg) != 0 # still ugly
    > "string".include? reg # this would seem to follow the ruby way, but it
    > throws a TypeError


    What am I not understanding here?

    "string" =~ reg


    Kirk Haines
    , Jan 14, 2007
    #3
  4. Rob Sanheim

    Rob Sanheim Guest

    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

    So of course, when it was both a valid query and had a valid length,
    it would return zero because it matched the start of the regexp. The
    caller of the method wanted true or false only, not zero.
    Rob Sanheim, Jan 14, 2007
    #4
  5. 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
    James Edward Gray II, Jan 14, 2007
    #5
  6. On Sun, Jan 14, 2007 at 01:09:49PM +0900, 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
    >
    > So of course, when it was both a valid query and had a valid length,
    > it would return zero because it matched the start of the regexp. The
    > caller of the method wanted true or false only, not zero.

    The caller is poorly written then :)
    To work around this:

    !!is_valid # !! is like the to boolean operator ;)
    end
    Logan Capaldo, Jan 14, 2007
    #6
  7. 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
    >
    > So of course, when it was both a valid query and had a valid length,
    > it would return zero because it matched the start of the regexp. The
    > caller of the method wanted true or false only, not zero.


    Who wrote a caller that wanted true or false only, and why?
    What does the caller do when it receives 0?

    irb(main):002:0> puts 'yeah' if true
    yeah
    => nil
    irb(main):003:0> puts 'yeah' if 0
    yeah
    => nil
    irb(main):004:0> puts 'yeah' if false
    => nil

    If you actually have a caller written so mindlessly that it
    only works with true or false:

    !!(is_valid = valid_query_length? && (query =~ regex) )
    William James, Jan 14, 2007
    #7
  8. William James wrote:

    > !!(is_valid = valid_query_length? && (query =~ regex) )


    Make that

    is_valid = !!(valid_query_length? && (query =~ regex) )
    William James, Jan 14, 2007
    #8
  9. Rob Sanheim

    Rob Sanheim Guest

    On 1/13/07, William James <> wrote:
    > 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
    > >
    > > So of course, when it was both a valid query and had a valid length,
    > > it would return zero because it matched the start of the regexp. The
    > > caller of the method wanted true or false only, not zero.

    >
    > Who wrote a caller that wanted true or false only, and why?
    > What does the caller do when it receives 0?
    >
    > irb(main):002:0> puts 'yeah' if true
    > yeah
    > => nil
    > irb(main):003:0> puts 'yeah' if 0
    > yeah
    > => nil
    > irb(main):004:0> puts 'yeah' if false
    > => nil
    >
    > If you actually have a caller written so mindlessly that it
    > only works with true or false:
    >
    > !!(is_valid = valid_query_length? && (query =~ regex) )


    Well I guess I was mindless, then. =) I wrote a spec that was using
    "should.be(true)" and "should.be(false)", which is using assert_same
    underneath, so of course it was failing if it returned 0. So the easy
    solution is to use should.equal instead of should.be. Thanks for the
    double !! tip, though.

    Now, about allowing regexp's for "include?" - doesn't that fit well
    with the humane interface of String?

    - Rob
    Rob Sanheim, Jan 14, 2007
    #9
  10. On 1/14/07, Rob Sanheim <> wrote:
    > On 1/13/07, William James <> wrote:
    > > 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
    > > >
    > > > So of course, when it was both a valid query and had a valid length,
    > > > it would return zero because it matched the start of the regexp. The
    > > > caller of the method wanted true or false only, not zero.

    > >
    > > Who wrote a caller that wanted true or false only, and why?
    > > What does the caller do when it receives 0?
    > >
    > > irb(main):002:0> puts 'yeah' if true
    > > yeah
    > > => nil
    > > irb(main):003:0> puts 'yeah' if 0
    > > yeah
    > > => nil
    > > irb(main):004:0> puts 'yeah' if false
    > > => nil
    > >
    > > If you actually have a caller written so mindlessly that it
    > > only works with true or false:
    > >
    > > !!(is_valid = valid_query_length? && (query =~ regex) )

    >
    > Well I guess I was mindless, then. =) I wrote a spec that was using
    > "should.be(true)" and "should.be(false)", which is using assert_same
    > underneath, so of course it was failing if it returned 0. So the easy
    > solution is to use should.equal instead of should.be. Thanks for the
    > double !! tip, though.
    >
    > Now, about allowing regexp's for "include?" - doesn't that fit well
    > with the humane interface of String?


    If you are using RSpec, you can simply say:
    "blah".should =~ /ah/
    or
    "blah".should_not =~ /z/

    If you want "include? with a Regexp" behavior, you can do:
    if some_string[/some pattern/]
    Wilson Bilkovich, Jan 14, 2007
    #10
  11. Rob Sanheim

    Rob Sanheim Guest

    On 1/14/07, Wilson Bilkovich <> wrote:
    > On 1/14/07, Rob Sanheim <> wrote:
    > > On 1/13/07, William James <> wrote:
    > > > 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
    > > > >
    > > > > So of course, when it was both a valid query and had a valid length,
    > > > > it would return zero because it matched the start of the regexp. The
    > > > > caller of the method wanted true or false only, not zero.
    > > >
    > > > Who wrote a caller that wanted true or false only, and why?
    > > > What does the caller do when it receives 0?
    > > >
    > > > irb(main):002:0> puts 'yeah' if true
    > > > yeah
    > > > => nil
    > > > irb(main):003:0> puts 'yeah' if 0
    > > > yeah
    > > > => nil
    > > > irb(main):004:0> puts 'yeah' if false
    > > > => nil
    > > >
    > > > If you actually have a caller written so mindlessly that it
    > > > only works with true or false:
    > > >
    > > > !!(is_valid = valid_query_length? && (query =~ regex) )

    > >
    > > Well I guess I was mindless, then. =) I wrote a spec that was using
    > > "should.be(true)" and "should.be(false)", which is using assert_same
    > > underneath, so of course it was failing if it returned 0. So the easy
    > > solution is to use should.equal instead of should.be. Thanks for the
    > > double !! tip, though.
    > >
    > > Now, about allowing regexp's for "include?" - doesn't that fit well
    > > with the humane interface of String?

    >
    > If you are using RSpec, you can simply say:
    > "blah".should =~ /ah/
    > or
    > "blah".should_not =~ /z/
    >
    > If you want "include? with a Regexp" behavior, you can do:
    > if some_string[/some pattern/]
    >


    Well the method under test (under spec?) wasn't dealing with regex.
    It was just using a regex to figure out if something was valid, and
    then checking some other things and returning what I thought would be
    true or false. So thats why I couldn't use the should =~ methods, and
    in the end really wanted should.equal (I'm using spec/unit, not
    rspec).

    - rob
    Rob Sanheim, Jan 14, 2007
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Boris Pelakh
    Replies:
    3
    Views:
    455
    Purl Gurl
    Apr 8, 2004
  2. Replies:
    3
    Views:
    1,552
  3. Aaron Scott
    Replies:
    7
    Views:
    361
    alex23
    Dec 2, 2008
  4. BrendanC
    Replies:
    8
    Views:
    183
    Brian Candler
    Jul 18, 2009
  5. Joao Silva
    Replies:
    16
    Views:
    342
    7stud --
    Aug 21, 2009
Loading...

Share This Page