[英]Regex: URL re-writing any path without extension
I've written a regex which "works" for all the test cases I can think of. 我写了一个正则表达式,它“适用于”我能想到的所有测试用例。 Essentially any URL which matches the pattern:
基本上任何匹配模式的URL:
/app.* AND does not have an extension of length 1-4 at the end should be re-written. /app.* AND没有长度为1-4的扩展名,应该重写。 I've come up with:
我想出来:
/app((?:\\/[\\w([^\\..]{1,4}\b)\\-]+)+)
The question is, can this be simplified to achieve the same thing? 问题是,这可以简化以实现同样的目的吗? Also, could I replace my use of \\w with something like .*, I could be wrong but I suspect as soon as I encounter a URL with an odd character it'll break.
另外,我可以用。*之类的东西替换我对\\ w的使用,我可能是错的但是我怀疑只要遇到一个带有奇怪字符的URL它就会破坏。
EDIT 1: Example URLs that should match: 编辑1:应匹配的示例网址:
/app AND /app/
/app/auth
/app/auth/fb
/app/auth/twitter
/app/groups
/app/conn/manage
/app/play
/app/play/migrate
/app/play/migrate/done
Example URLs that should NOT match: 不匹配的示例网址:
/app/js/some.file.js
/app/js/jquery.js
/app/styles/default/rain.css
/app/styles/name/file.css
/app/tpl/index.tpl
/app/tpl/file.html
/app/tpl/some.other.tpl
Thanks. 谢谢。
I think a better approach is to put all the assets you want the webserver to handle in a single directory. 我认为更好的方法是将您希望Web服务器的所有资产放在一个目录中。 Like
/app/public
, so you would get app/public/js
and app/public/html
etc. This will make you have no edge cases and a far easier URL handling. 喜欢
/app/public
,所以你会得到app/public/js
和app/public/html
等。这将使你没有边缘情况和更容易的URL处理。
Anyway, I think the regex below answers the question you asked: match anything except if there is a extension with 1 - 4 characters on the file. 无论如何,我认为下面的正则表达式回答了你问的问题:匹配任何东西,除非文件上有1到4个字符的扩展名。
^(\/(\w+))*\/?(\.\w{5,})?\??([^.]+)?$
http://rubular.com/r/4CQ4amccH5 http://rubular.com/r/4CQ4amccH5
^ //start of anchor
(
\/ //match forward slash
(\w+) //match any word character, match atleast once
)+ //match this group atleast once (this group captures /app/etc/etc)
\/? //match a forward slash, make it optional (to also capture /app/)
(\.\w{5,})? //match any word after a . with 5 characters or more, make it optional
\?? //match a ?, make the match optional
([^.]+)? //match anything not containing a . 1 or more times, make the match optional
$ //end of anchor
This still needs some work to make it work in Java, mainly alot of escaping escape characters. 这仍然需要一些工作才能使它在Java中工作,主要是逃避转义字符。
Your regex would be: 你的正则表达式是:
/app(/\w+)*/?$
I assumed you want to match a url with word characters that may end up with a slash but not a file extension. 我假设你想要一个url与单词字符匹配,这些单词字符可能以斜杠而不是文件扩展名结尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.