Ruby: Return boolean for regex comparison

Updated . Posted . Visible to the public.

A collection of code snippets which return a boolean value for a regex comparison.

regexp.match?(string) # Recommended for Ruby >= 2.4

!!(string =~ regexp)  # Recommended for older Rubies

regexp === string

!(regexp !~ string)

The Ruby 2.4 method Regexp#match? Show archive.org snapshot does not set globals like $~ or $1, so it should be more performant.

Last edit
Henning Koch
License
Source code in this card is licensed under the MIT License.
Posted by Emanuel to makandra dev (2017-02-09 13:29)