我需要匹配以下模式:
#/a/b/c/d
#/a/b/&1/d
#/a/b/c[&1]/d适用下列规则:
a) # is the number sign and then its a path. Pretty much anything can be in the path segments. For &1 and []'s, they follow certain rules.
b) &1 (or any number) has to be in a path segment by itself
c) [&1] has to follow at least one character and has to end the segment, only [&l1] is allowed for now所以,我想出了以下几点:
^#((/[^/&]+)|(/&\\d+)|(/[^/]+\\[&1\\]))+看起来很有效,但我的分析显示这是个瓶颈。是否有一种提高性能或以更优化的方式对其进行重组的方法?我不需要捕获或分组任何东西,我只需要知道这是否是一条有效的路径。
发布于 2017-06-22 23:42:21
运行一些快速测试,这是最快的。我在负字符类中添加了一些括号,以排除其中包含额外括号的路径。没有它们它会更快。
var pattern = "^#(?:/(?:&\\d+|[^/&[\\]]+\\[&1]|[^/&[\\]]+))+$";
var REc = new Regex(pattern, RegexOptions.Compiled);根据最频繁的段类型更改顺序可能更快--这在我的测试数据中更快,这些测试数据大多有字母数字段:
var pattern2 = "^#(?:/(?:[^/&[\\]]+|&\\d+|[^&/[\\]]+\\[&1]))+$";使用REc.IsMatch(bs)进行测试
如果分段括号没有问题,这会更快:
var pattern = "^#(?:/(?:&\\d+|[^/]+\\[&1]|[^/&]+))+$";发布于 2017-06-22 23:10:59
您可以尝试的一件事是告诉regex引擎不要捕获任何东西:
^#(?:(?:/[^/&]+)|(?:/&\\d+)|(?:/[^/]+\\[&1\\]))+通过标记每个组(?: ... ),我们告诉引擎忽略这个组。
https://stackoverflow.com/questions/44710647
复制相似问题