Regexp一个或多个vs n + 1匹配

时间:2017-05-29 01:20:35

标签: regex svg go re2

我发现我的go项目中的regexp替换速度非常慢,\ s +使用regexp.ReplaceAllLiteralString(re2)替换为单个空格。

显然在SVG中存在崩溃的空白,不管这是不是一个好主意,当改为“{2,}”时,操作只有时间的十分之一。

虽然使用\ s vs''是一个公平的区别 - 但这是有道理的,它是比较的5倍。但是为什么当使用单个空格和+时,它比使用{2,}?

慢9倍

1 个答案:

答案 0 :(得分:0)

X+ means "every instance of X that occurs once or more", which means it's equivalent to {1,}. You're replacing (a single blank) as well as       when using \s+.

For an SVG document, or any XML document for that matter, using \s+ is bound to be slow. The following document would require 5 replacements, ignoring newline sequences:

<?xml version="1.0" encoding="utf-8"?>
<element attr1="value" attr2="value" attr3="value"/>

Now imagine an SVG document that is so much more complex, and it's reasonable that something like \s{2,} would be a lot faster than \s+ (a.k.a. \s{1,})! There'd be 0 replacements in the document above if you used \s{2,}!