Skip to content

Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →

class method · added in Ruby 3.2

Regexp.linear_time?

Regexp.linear_time?(re)
Regexp.linear_time?(string, options = 0)

Returns true if matching against re can be done in linear time to the input string.

Example
Regexp.linear_time?(/re/) # => true

Note that this is a property of the ruby interpreter, not of the argument regular expression. Identical regexp can or cannot run in linear time depending on your ruby binary. Neither forward nor backward compatibility is guaranteed about the return value of this method. Our current algorithm is (*1) but this is subject to change in the future. Alternative implementations can also behave differently. They might always return false for everything.

(*1): doi.org/10.1109/SP40001.2021.00032

Ruby version changes

  1. 3.2
See also .last_match .new .json_create .quote also in 4.1 · 4.0 · 3.4 · 3.2