简体   繁体   English

正则表达式排除URL的两个斜杠之间的字符串

[英]Regex excluding strings in between two slashes on URL

I have a regex like this basename/(.+)/(.+)/(.+)/?$ but the problem is this is a greedy regex. 我有这样的正则表达式,如基basename/(.+)/(.+)/(.+)/?$但问题是这是一个贪婪的正则表达式。 I want to exclude certain strings on the 2nd match such that if it matches one of the blacklisted strings, it will return false.. 我想在第二个匹配项中排除某些字符串,以便如果它与列入黑名单的字符串之一匹配,它将返回false。

This problem is connected to my other question on https://wordpress.stackexchange.com/questions/102246/wordpress-returns-404-on-custom-rewrite-rules the only diffence is the approach. 此问题与https://wordpress.stackexchange.com/questions/102246/wordpress-returns-404-on-custom-rewrite-rules上的其他问题有关,唯一的区别是方法。

Hope you guys can help me. 希望你们能帮助我。 Thanks! 谢谢!

EDIT: here is what im trying to work from: basename/(.+)/!(string1|string2|string3).*/(.+)/?$ (added from comments) 编辑:这是我正在尝试的工作方式: basename/(.+)/!(string1|string2|string3).*/(.+)/?$ (从注释中添加)

PS I've seen this but this doesn't seem like what I wanted. 附注:我已经看到了这一点,但这似乎与我想要的不一样。

My first if/else regex condition, cool right :) ? 我的第一个if / else正则表达式条件很酷:)?
basename\\/(.+)\\/(?(?=(?:bar|string2|string3)\\/)^|(.+))\\/(.+)\\/?$

So what does this mean ? 那么这是什么意思 ?

  • basename\\/ : match basename/ basename\\/ :匹配basename/
  • (.+)\\/ : anything until / found and match / (.+)\\/ :直到/找到并匹配/
  • (?(?=(?:bar|string2|string3)\\/)^|(.+)) : the tricky part, we'll split this up: (?(?=(?:bar|string2|string3)\\/)^|(.+)) :棘手的部分,我们将其拆分:
    • (? : This means if (? :这意味着如果
    • (?=(?:bar|string2|string3)\\/) : positive lookahead, check if there is bar/ or string2/ , or string3/ . (?=(?:bar|string2|string3)\\/) :正向查找,检查是否存在bar/string2/string3/
    • ^ : If the lookahead succeeded, then match this. ^ :如果前瞻成功,则进行匹配。 ^ means begin of line, since we are in the middle of a string, this will always return false ! ^表示行的开头,因为我们在字符串的中间,所以它将始终返回false!
    • |(.+) : Else, match everything until ... |(.+) :否则,匹配所有内容,直到...
    • To sum it up, we check if there is bar/ or string2/ or string3/ , if there is a match then we'll look for begin of line ^ which will always return false (our intention) else match further (.*) 总结一下,我们检查是否存在bar/string2/string3/ ,如果存在匹配项,那么我们将寻找第^行的开头,该行将始终返回false(我们的意图),否则将进一步匹配(.*)
  • \\/(.+)\\/?$ : match / , then anything until / optionally. \\/(.+)\\/?$ :匹配/ ,然后匹配任何东西,直到/可选。 $ means end of line. $表示行尾。

Online demo 在线演示

You'll want to make catch group that has the same pattern (/dir) lazy matches (using ?). 您将要使具有相同模式(/ dir)延迟匹配(使用?)的捕获组。

For strings such as: 对于诸如以下的字符串:

basename/dir1/dir2/dir3/
basename/dir1/string1/dir3/  
basename/dir1/string2/dir3/

The following Regex would return false for the second two but catch the first: 以下正则表达式将在后两个返回false,但捕获第一个:

basename/(.+?)/(?!string1|string2|string3)(.+?)/(.+?)/$

PHP fiddle: http://phpfiddle.org/lite/code/1qr-iip PHP小提琴: http//phpfiddle.org/lite/code/1qr-iip

you could use .*? 您可以使用.*? instead of .* to make it ungreedy, but you'd probably be better with a greedy match that excludes / . 而不是.*使其变得不贪心,但最好使用排除/的贪心匹配。 ie [^/]* . [^/]*

Your question is not all that clear. 您的问题不是很清楚。 I'm not sure what you're doing with the !(x|y|z) construct. 我不确定您正在使用!(x|y|z)构造函数。 That ! ! is a literal. 是文字。 Is that what you intended? 那是你想要的吗?

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

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