52

How do I a string against a regex such that it will return true if the whole string matches (not a substring)?

eg:

test( \ee\ , "street" ) #=> returns false
test( \ee\ , "ee" ) #=> returns true!

Thank you.

3
  • 24
    Give a man a string comparison and he'll be true, give a man a regex and he'll lose all ability to reason and search the internet. Commented Feb 10, 2010 at 12:25
  • @doctororange, would you be so kind as to accept Tomas Markauskas'answer? I can't remove my answer since you accepted it, buty I forgot Ruby handles (some) regex meta characters differently than most regex implementations. Thanks. Commented Feb 10, 2010 at 12:38
  • Done. Thank you both for your replies. Commented Feb 11, 2010 at 14:11

3 Answers 3

74

You can match the beginning of the string with \A and the end with \Z. In ruby ^ and $ match also the beginning and end of the line, respectively:

>> "a\na" =~ /^a$/
=> 0
>> "a\na" =~ /\Aa\Z/
=> nil
>> "a\na" =~ /\Aa\na\Z/
=> 0
Sign up to request clarification or add additional context in comments.

2 Comments

You want to use \z instead of \Z to match the entire string. "hi\n" =~ /\Ahi\Z/
The return value (when not nil) is the index of the first character of the first match.
19

This seems to work for me, although it does look ugly (probably a more attractive way it can be done):

!(string =~ /^ee$/).nil?

Of course everything inside // above can be any regex you want.

Example:

>> string = "street"
=> "street"
>> !(string =~ /^ee$/).nil?
=> false
>> string = "ee"
=> "ee"
>> !(string =~ /^ee$/).nil?
=> true

Note: Tested in Rails console with ruby (1.8.7) and rails (3.1.1)

Comments

0

So, what you are asking is how to test whether the two strings are equal, right? Just use string equality! This passes every single one of the examples that both you and Tomas cited:

'ee'   == 'street' # => false
'ee'   == 'ee'     # => true
"a\na" == 'a'      # => false
"a\na" == "a\na"   # => true

7 Comments

This is true, but a regular expression also allows you to expand your definition of 'equal' to things like /^(ee|oo)$/ pretty nicely.
@Timo Lehto: It passes every single specification the OP has provided, plus every example in the other answer, and it is much simpler than the other answer. I don't see what's wrong with that.
I do not want flood with useless babbling, so I'll just say one more thing. The question was clearly about regexps. I dunno why the "silly" examples. While your answer may or may not serve as a good pointer for doc, I can not see how anyone finding his way here in search for an answer to the question could in any ways benefit of it. And that's why I was so amused by the fact that it seems your answer has helped 5 people to learn the fact that they do not need to use regexps for string equality checks. xD
I think you misunderstood the author. I think he meant that the whole string must match the regex, not a substring (i.e. by using ^<regex>$ or similar)
@Ctx: the OP's Regexps do not actually make use of any Regexp features at all. There are no alternations, no repetitions, no wildcards, no character classes, no capture groups, no references, nothing. It just doesn't make any sense whatsoever to use Regexps in the first place. My answer passes every single test in the OP's question as well as every single test in the accepted answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.