小编Yah*_*ntu的帖子

Slow Ruby Regex Becomes Fast with Odd Change

I've been debugging a site to find the source of long page loading times, and I've narrowed it down to a regex that's used to extract URLs from text:

/(?:([\w+.-]+):\/\/|(?:www\.))[^\s<]+/g
Run Code Online (Sandbox Code Playgroud)

This takes about 3 seconds to run on a large block of text. I found out that if I add the inverse of the first clause to the start of the regex ((?:[^\w+.-]|^)), it runs almost instantly:

/(?:[^\w+.-]|^)(?:([\w+.-]++):\/\/|(?:www\.))[^\s<]+/gx
Run Code Online (Sandbox Code Playgroud)

It seems to me like the added clause shouldn't affect …

ruby regex pcre

8
推荐指数
1
解决办法
232
查看次数

标签 统计

pcre ×1

regex ×1

ruby ×1