Searching for a Regular Expression which follows “(/(?:[a-zA-Z0-9_-]+/?)*)” AND does not start with “/xyz”

时间:2022-05-08 21:14:04

I m currently working on a python-project with webapp2 where you map a URL (defined by a regular expression) to a specific handler-class. (This class handles the URL-request and responds to it)

我目前正在使用webapp2进行python项目,您可以将URL(由正则表达式定义)映射到特定的处理程序类。 (此类处理URL请求并响应它)

Currently all requests following the regular expression :

目前所有请求都遵循正则表达式:

(/(?:[a-zA-Z0-9_-]+/?)*)

are mapped to my HomeHandler() class.

映射到我的HomeHandler()类。

Now i need to change the regex to follow:

现在我需要更改正则表达式:

URL DOES NOT START with "/xyz"  AND followed by "(/(?:[a-zA-Z0-9_-]+/?)*)"

Example:

例:

/xyz/abcdefABCD2341/488888/edit -> should NOT be matched
/notxyz/abcdefABCD2341/488888/edit -> should be matched

Does anyone knows how to solve this problem?

有谁知道如何解决这个问题?

Thanks

谢谢

4 个解决方案

#1


3  

This should solve your problem... I've inserted negative lookahead that checks for xyz and added ^ and $ so it checks the entire string matches, otherwise your regex is unchanged

这应该可以解决你的问题......我插入负向前瞻,检查xyz并添加^和$所以它检查整个字符串匹配,否则你的正则表达式没有变化

^(/(?!xyz)(?:[a-zA-Z0-9_-]+/?)*)$

Your test case, change the start to xyz or not

您的测试用例,将开始更改为xyz

#2


2  

I think the "negative lookahead assertion" can help you. Go to the link below and search for "negative lookahead assertion". https://docs.python.org/3.4/library/re.html In your case, you may try:

我认为“负面前瞻断言”可以帮助你。转到下面的链接,搜索“负向前瞻断言”。 https://docs.python.org/3.4/library/re.html在您的情况下,您可以尝试:

^(?!/xyz)(/(?:[a-zA-Z0-9_-]+/?)*)

#3


2  

The other answer regex's are correct, but for Google App Engine, the app.yaml is order dependent, so you can add handlers to cover your /xyz case, then a handler for everything else.

另一个正则表达式是正确的,但对于Google App Engine,app.yaml依赖于顺序,因此您可以添加处理程序来覆盖/ xyz案例,然后处理其他所有内容的处理程序。

So for your siutation:

所以为了你的siutation:

# app.yaml
handlers:

- url: /xyz.*
  script: xyzHandler.app

- url: .*
  script: homeHandler.app

#4


0  

This should probably be just:

这应该只是:

^/(?!xyz)[\w/-]+$

#1


3  

This should solve your problem... I've inserted negative lookahead that checks for xyz and added ^ and $ so it checks the entire string matches, otherwise your regex is unchanged

这应该可以解决你的问题......我插入负向前瞻,检查xyz并添加^和$所以它检查整个字符串匹配,否则你的正则表达式没有变化

^(/(?!xyz)(?:[a-zA-Z0-9_-]+/?)*)$

Your test case, change the start to xyz or not

您的测试用例,将开始更改为xyz

#2


2  

I think the "negative lookahead assertion" can help you. Go to the link below and search for "negative lookahead assertion". https://docs.python.org/3.4/library/re.html In your case, you may try:

我认为“负面前瞻断言”可以帮助你。转到下面的链接,搜索“负向前瞻断言”。 https://docs.python.org/3.4/library/re.html在您的情况下,您可以尝试:

^(?!/xyz)(/(?:[a-zA-Z0-9_-]+/?)*)

#3


2  

The other answer regex's are correct, but for Google App Engine, the app.yaml is order dependent, so you can add handlers to cover your /xyz case, then a handler for everything else.

另一个正则表达式是正确的,但对于Google App Engine,app.yaml依赖于顺序,因此您可以添加处理程序来覆盖/ xyz案例,然后处理其他所有内容的处理程序。

So for your siutation:

所以为了你的siutation:

# app.yaml
handlers:

- url: /xyz.*
  script: xyzHandler.app

- url: .*
  script: homeHandler.app

#4


0  

This should probably be just:

这应该只是:

^/(?!xyz)[\w/-]+$