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

前端 未结 7 1499
独厮守ぢ
独厮守ぢ 2020-12-13 05:07

What is the fastest way to check if a string matches a regular expression in Ruby?

My problem is that I have to \"egrep\" through a huge list of strings to find whic

7条回答
  •  青春惊慌失措
    2020-12-13 06:11

    Depending on how complicated your regular expression is, you could possibly just use simple string slicing. I'm not sure about the practicality of this for your application or whether or not it would actually offer any speed improvements.

    'testsentence'['stsen']
    => 'stsen' # evaluates to true
    'testsentence'['koala']
    => nil # evaluates to false
    

提交回复
热议问题