简体   繁体   English

正则表达式:URL重写任何没有扩展名的路径

[英]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/jsapp/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.

相关问题 Tapestry5:URL重写:将参数传递给transformPageRenderLink方法 - Tapestry5 : URL Re-writing : Pass parameters to transformPageRenderLink method 仅更改TIFF标头,而无需重写图像内容 - Change TIFF header only without re-writing image content 重新写入Java txt文件 - Re-Writing to java txt files 将for循环重写为while循环 - Re-writing a for loop to a while loop 使用Apache-poi将工作表添加到现有的excel文件中而不重写整个文件 - Adding sheets to existing excel file without re-writing complete file, using Apache-poi 有没有办法将相同的 object 添加到 ArrayList,但更改值而不重写 ArrayList 的其他元素? - Is there a way to add the same object to ArrayList, but with changed values without re-writing the other elements of ArrayList? 在dynamodb插入过程中重写相同的数据 - re-writing the same data during insertion in dynamodb 用逗号将文件输入分割成Java数组,然后重新写入文件 - Split file input by comma into an array Java, and re-writing to the file Hadoop(1.1.2)XML处理和重写文件 - Hadoop (1.1.2) XML processing & re-writing file 正则表达式从路径获取带或不带扩展名的文件名 - Regex to get filename with or without extension from a path
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM