Fastest Way to Check If a String Matches a Regexp in Ruby

Fastest way to check if a string matches a regexp in ruby?

Starting with Ruby 2.4.0, you may use RegExp#match?:

pattern.match?(string)

Regexp#match? is explicitly listed as a performance enhancement in the release notes for 2.4.0, as it avoids object allocations performed by other methods such as Regexp#match and =~:

Regexp#match?

Added Regexp#match?, which executes a regexp match without creating a back reference object and changing $~ to reduce object allocation.

How to check if the string matches a regex

=== is the subsumption operator, where big_concept === specific_concept tests whether the big_concept subsumes the specific_concept. With regular expressions, you want to test whether the class of strings described by a regular expression subsumes a specific string; so you would write it as rule === "1t". This usage is described at RegExp#===.

"1t" === rule tests whether the string "1t" subsumes the regular expression; but strings only subsume (by Ruby's rules) objects that are identical to the string when stringified (with to_s). rule stringifies to "(?-mix:^[0-9][a-z]$)", which is different from "1t", therefore false. This usage is described at String#===.

With that, here's your code, corrected (and expanded :D ):

rule = /^[0-9][a-z]$/

puts "rb -------------------------"
p "rb".match? rule #=> false
p !!("rb".match? rule) #=> false
p !!("rb" =~ rule) #=> false
p !("rb" !~ rule) #=> false
p rule === "rb" #=> false
p !!("rb"[rule]) #=> false
p rule.match? "rb" #=> false
p !!(rule.match "rb") #=> false

puts "1t -------------------------"
p "1t".match? rule #=> true
p !!("1t".match rule) #=> true
p !!("1t" =~ rule) #=> true
p !("1t" !~ rule) #=> true
p rule === "1t" #=> true
p !!("1t"[rule]) #=> true
p rule.match? "1t" #=> true
p !!(rule.match "1t") #=> true

Boolean check if a string matches a regex in ruby?

You can use this:

def test(s)
/Macintosh/ === s
end

Ruby =~to check if string matches pattern

What is wrong is that you are actually checking:

  • whether a string contains any number or letter,

but you wanted to check:

  • whether a string does not contain anything other than a number or letter.

You can check it like this:

def valid?
"abc#d" !~ /\P{Alnum}/
end

fastest and elegant way to check whether a given list contains some element by regular expression

As Loocid suggested, you can use any. I would do it with a generator expression like so:

newlist = ['this','thiis','thas','sada']
regex = re.compile('th.s')

result = any(regex.match(word) for word in newlist)
print(result) # True

Here is another version with map that is slightly faster:

result = any(map(regex.match, newlist))

Checking whether a string matches any regex

Don't use an array of regexes; instead, use a search tree.

Here's a great intro article: Fast Algorithms for Sorting and Searching Strings.

  • http://www.cs.tufts.edu/~nr/comp150fp/archive/bob-sedgewick/fast-strings.pdf

Or if you prefer a quick-and-dirty solution, you could use Ruby Regexp#union to fuseregexps together to create one big one. This will likely be more efficient at detecting when none of the strings match, and you can benchmark it. If it matches, then you use the match position to figure out which of the regexps matched.

  • http://www.ruby-doc.org/core-1.9.3/Regexp.html#method-c-union

(Thanks to comment from "mu is too short" for the #union method)

For your particular case that you describe, where all the regexps start with "pre" and an optional colon, etc. then you can do /pre:? +(star|moon|sun)/ and use the match result to find out which matched.

Ruby's Regexp is implemented using a search tree; there's an interesting description that may give you leads here:

  • http://patshaughnessy.net/2012/4/3/exploring-rubys-regular-expression-algorithm

Rails 3 - Check if string/text includes a certain word/character via regex in controller

I do ruby regex'es this way:

stringObj.match(/regex/)


Related Topics



Leave a reply



Submit