为什么规则在扩展字符 类 时停止工作?

Why does the rule stop working when expanding a character classes?

我有测试网址:

http://host.com/a/123/321/123
http://host.com/A/12z3/3G21
http://host.com/a/123
http://host.com/A_B/12z3/3G21
http://host.com/A_B1/12z3/3G21
http://host.com/A-B1/12z3/3G21

解析规则:

/^[a-z]+:\/\/(?<host>.+)\/(?<uid>[a-z]+)(\/(?<var1>\w+))(\/(?<var2>\w+))?(\/(?<var3>\w+))?\/?$/i

现在规则parses correctly从第一个到第三个网址:

但是当我在带有下划线和破折号的 URL 上使用更多 类 (?<uid>[a-z0-9-_]+) I miss the last two captured group 扩展 (?<uid>[a-z]+) 时:

我的规则有什么问题?

您遇到的问题是主机在其匹配项中使用了 UID。所以你可以做的是让它在到达第一个正斜杠时停止,不要让它超过那个。

这将使 (?<uid>[a-z0-9-_]+) 可以按照您的意愿工作。

对于主机,请保留:(?<host>.[^\/]+)

你可以看看here