简体   繁体   中英

A simple regex that is performing very badly on 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM