这个正则表达式是否可以优化(减少匹配步骤)

时间:2014-07-04 21:59:07

标签: regex preg-match pcre

需要处理的要求:

only ascii letters in lower register [a-z], digits [0-9], "/" and "-"
the string should begin with "/"
"/" and "-" should not stand next to each other nor self in ANY combination 
string must end with a ascii letters in lower register [a-z], digits [0-9] and "/"

到目前为止:

^((?:(?![0-9a-z\/\-]*?(\/|\-)(\/|\-)))\/[0-9a-z\/\-]*?[0-9a-z\/]$)

链接到调试regex debugger

1 个答案:

答案 0 :(得分:1)

如果我理解你的要求,你可以使用这种模式:

\A/(?>[a-z0-9]+[-/])*[a-z0-9]++/?\z