Regex 将URL与最多一定数量的斜杠匹配

Regex 将URL与最多一定数量的斜杠匹配,regex,Regex,有没有办法用一个正则表达式匹配下面的所有表达式?如果没有,什么是好的/干净的方法 /resources/$ /resources/type$ /resources/type/$ /resources/category$ /resources/category/$ /resources/anything here that includes up to 1 slash and ends with that slash 以下内容不应匹配,因为存在某些内容: /resources/type/some

有没有办法用一个正则表达式匹配下面的所有表达式?如果没有,什么是好的/干净的方法

/resources/$
/resources/type$
/resources/type/$
/resources/category$
/resources/category/$
/resources/anything here that includes up to 1 slash and ends with that slash
以下内容不应匹配,因为存在
某些内容

/resources/type/something
/resources/category/something

这应该满足您的要求:

^\/([^\/]*\/?){,2}$
已编辑错误:p

您可以使用以下选项:

^\/resources(\/[a-z]*\/?)?$

这错误地匹配了“/resources/test/something”,谢谢,但这也匹配了以
/resources/
以外的内容开头的URL,我正在努力调整它。真的。在我的辩护中,您的问题中没有任何内容表明除了/resources/之外的URL不应该被匹配(尽管您可以看到它是隐含的)。我已经在“不应该匹配”部分下添加了一个非/resources/URL,以获得更清晰的问题。谢谢。我还调整了大写字母、数字和破折号<代码>^\/资源(\/[a-zA-Z0-9-]*\/?)?$