因此,我有以下用于语法突出显示的Regex:
static Regex cKeyWords = new Regex("(\t|\r\n|\\s|\\(|\\)|^)(auto|break|c(ase|har|onst|ontinue)|d(efaut|ouble)|e(lse|num|xtern)|f(loat|or)|goto|i(f|nt)" +
"|long|re(gister|turn)|s(hort|igned|izeof|tatic|truct|witch)|typedef|u(nion|nsigned)|v(oid|olatile)|while)(?=\t|\r\n|\\s|\\(|\\)|{|}|$)", RegexOptions.Compiled);它可以做我想做的事情,但是当涉及到大约2000个字符的大型文件时,它需要6秒多一点的时间。
是否有提高性能的方法?
编辑:在仔细查看了所有评论/答案/技巧之后,我现在有了以下内容:
static Regex cKeyWords = new Regex(@"\b(?:
s(?:hort|i(?:gned|zeof)|t(?:atic|ruct)|witch) | c(?:ase|har|o(?:nst|ntinue)) |
e(?:lse|num|xtern) | i(?:f|nt) | f(?:loat|or) | d(?:efault|ouble) | un(?:ion|signed) |
re(?:gister|turn) | vo(?:id|latile) | while | break | long | typedef | auto | goto
)\b",
RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace);这个可以在5.5秒内处理2000个字符的长文本。这样好多了。不过,我会继续做一些测试,看看能否进一步缩短时间。
发布于 2015-05-04 20:03:32
在我看来,开头的(\t|\r\n|\\s|\\(|\\)|^)和结尾的(?=\t|\r\n|\\s|\\(|\\)|{|}|$)都是无用的,可以用单词边界来代替相同的结果。(使用交替启动模式是您应该避免的最糟糕的事情之一,因为regex引擎必须测试字符串中的每个位置,在最坏的情况下必须使用所有选项)
只在需要时使用捕获组,因为它们不需要使用内存和时间。在目前的情况下,你根本不需要它们。
所以你可以像这样重写你的模式:
static Regex cKeyWords = new Regex(@"\b(?:
auto | break | c(?:ase|har|onst|ontinue) | d(?:efaut|ouble) |
e(?:lse|num|xtern) | f(?:loat|or) | goto | i(?:f|nt) | long |
re(?:gister|turn) | s(?:hort|igned|izeof|tatic|truct|witch) | typedef |
un(?:ion|signed) | vo(?:id|latile) | while )\b",
RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace);注意,关键字现在在组0中(整个匹配)。
其他你可以尝试的事情:
c(?:ase|har|on(?:st|tinue))等。s(?:hort|igned|izeof|tatic|truct|witch)放在首位。(?=[a-gilr-w]) (所以所有关键字的第一个字母),或者至少在第一个单词边界之后立即添加(?=[a-z]) (请记住,单词边界可以在单词字符位置或非单词字符位置成功)。目的是避免在单词边界位置没有有趣的字母时测试替换。https://stackoverflow.com/questions/30037700
复制相似问题