简体   繁体   English

如何在app.yaml中使用正则表达式处理虚拟子目录?

[英]How can I handle virtual subdirectories with regex in app.yaml?

I'd like to point all of my visitors to "single subdirectories" to one page, and all visitors to "double subdirectories" to another. 我想将我的所有访问者都指向一个页面的“单个子目录”,并将所有访问者都指向另一个页面的“双子目录”。 Eg: 例如:

/foo/
/new/
/north/
/1-j4/

Would all point to 1.app, whereas 都指向1.app,而

/foo/bar/
/new/york/
/north/west/
/1-j4/a_990/

Would all point to 2.app. 都指向2.app。

I figured I could do this with non-greedy regex matching, like so: 我想可以通过非贪婪的正则表达式匹配来做到这一点,就像这样:

- url: /(.*?)/$
  script: 1.app

- url: /(.*?)/(.*?)/$
  script: 2.app

To my confusion, both /foo/ and /foo/bar/ resolve to script 1.app. 令我感到困惑的是,/ foo /和/ foo / bar /都解析为脚本1.app。 Does the "lazy" regex force itself up to include the middle /, since that's the only way to get a match? 因为这是获得匹配项的唯一方法,“惰性”正则表达式是否会强制自己包含中间/? How else can I do this? 我还能怎么做? I have tried using (\\w*?) but get the same result. 我尝试使用(\\ w *?),但得到相同的结果。

The .*? .*? will still match through any amount of / because . 仍将通过/匹配,因为. matches any character but a line break char (by default). 匹配除换行符以外的任何字符(默认情况下)。 You need to base your regexps on a negated character class, [^/]* , that matches 0 or more chars other than / . 您需要将正则表达式基于否定的字符类[^/]* ,它匹配/以外的0个或多个字符。

To match directories with one part, use ^([^/]*)/?$ and to match those with 2, use ^([^/]*)/([^/]*)/?$ . 要用一个部分匹配目录,请使用^([^/]*)/?$ ,要用2匹配那些目录,请使用^([^/]*)/([^/]*)/?$

Note that if you plan to use the patterns in online Web testers, you will have to escape / in most of them as by default they use / symbol as a regex delimiter. 请注意,如果您打算在在线Web测试人员中使用这些模式,则必须将/中的大多数转义,因为默认情况下,它们使用/符号作为正则表达式定界符。

Yes, the (.*?) includes slashes, so will resolve to 1.app . 是的, (.*?)包含斜杠,因此将解析为1.app If you put the 2.app handler first, it should do what you want: 如果将2.app处理程序放在第一位,则它应该执行您想要的操作:

- url: /(.*?)/(.*?)/$
  script: 2.app

- url: /(.*?)/$
  script: 1.app

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM