Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A simple regex that is performing very badly on Ruby

Tags:

regex

ruby

I have a simple Ruby regex that is taking very long to compute:

"fußball "*20 =~ /^([\S\s]{1000})/i

If I remove /i flag it works very fast. Why is it running so slowly? (I didn't wait for execution to finish)

I know this regex might not make sense, but I'm wondering what is under the hood.


Bug report: https://bugs.ruby-lang.org/issues/14418

like image 261
Kuba Avatar asked Jan 29 '18 14:01

Kuba


1 Answers

By default . doesn't match newlines. [\s\S] is a hack around that problem. In Ruby you can use the /m flag to make the dot match all characters. It's in the documentation Ruby Metacharacters and Escapes

[\S\s] for an unknown reason is very slow but you can change it to "fußball "*20 =~ /^(.{1000})/mi that does the same but faster

like image 167
Eduardo Clemens Avatar answered Nov 07 '22 16:11

Eduardo Clemens