简体   繁体   English

以特定字符串开头的正则表达式,并忽略包含特定字符串的表达式

[英]Regular expression that starts with a certain string and ignores those that contain a specific string

I am trying to write a regular expression in PHP that would match any string that contains 'search/site/*' but only if it does not contain the string 'ss_language' 我试图在PHP中编写一个正则表达式,它匹配任何包含'search / site / *'的字符串,但前提是它不包含字符串'ss_language'

Here is what I have so far: http://regexr.com?3416i This successfully matches any 'search/site/*' including 'search/site' so now I need the bit that excludes any string that contains 'ss_language' 这是我到目前为止: http ://regexr.com?3416i这成功匹配任何'搜索/网站/ *',包括'搜索/网站'所以现在我需要的位不包括任何包含'ss_language'的字符串

Thinking about this, it seems that I could just nest another preg_match to just look for the ss_language, but is that the best way to go about it vs. one single regular expression? 考虑到这一点,似乎我可以嵌套另一个preg_match来寻找ss_language,但这是与单个正则表达式相比的最佳方式吗? Is this even possible with one regular expression? 一个正则表达式甚至可以实现这一点吗? Here are some examples and what they should return: 以下是一些示例以及它们应返回的内容:

  • Pass - search/site 通行证 - 搜索/网站
  • Pass - search/site/cat 通行证 - 搜索/网站/猫
  • Pass - search/site/dog?f[0]=im_field_technology%3A20858 通过 - 搜索/网站/狗?f [0] = im_field_technology%3A20858
  • Pass - search/site/cat%20dog?f[0]=im_field_technology%3A20858 Pass - search / site / cat%20dog?f [0] = im_field_technology%3A20858
  • Fail - search/site/cat%20dog?f[0]=ss_language%3Aen 失败 - 搜索/网站/ cat%20dog?f [0] = ss_language%3Aen
  • Fail - search/site/?f[0]=im_field_technology%3A20858&f[1]=ss_language%3Aen 失败 - 搜索/网站/?f [0] = im_field_technology%3A20858&f [1] = ss_language%3Aen
  • Fail - search/site/?f[1]=ss_language%3Aen&f[0]=im_field_technology%3A20858 失败 - 搜索/网站/?f [1] = ss_language%3Aen&f [0] = im_field_technology%3A20858
  • Pass - search/site/?f[0]=im_field_technology%3A20858&f[1]=im_field_technology%3A20875o 通过 - 搜索/网站/?f [0] = im_field_technology%3A20858&f [1] = im_field_technology%3A20875o
  • Fail - node/23/edit 失败 - node / 23 / edit

Thanks in advance for any help. 在此先感谢您的帮助。 I'm pretty new to these. 我对这些很新。 Here is a failed attempt: http://regexr.com?3416f 这是一次失败的尝试: http//regexr.com?3416f

You can use a negative lookahead to assert that the string does not contain ss_language 您可以使用否定前瞻来断言该字符串不包含ss_language

^search/site/?(?!.*ss_language.*).*$

If you just want a true/false answer to whether or not it matches then you don't really need the .*$ (it would then only match search/site with the optional / given there is no ss_language anywhere in the string (short version): 如果你只是想要一个真实/错误的答案,它是否匹配,那么你真的不需要.*$ (它只会匹配search/site与可选/如果字符串中没有ss_language (短)版):

^search/site/?(?!.*ss_language)

You can check http://regexr.com?3417a for following regex: 您可以查看http://regexr.com?3417a以获取以下正则表达式:

^search/site/?(?:(?!ss_language).)*$

It's indeed using negative lookahead. 它确实使用负面的前瞻。

You don't need to use a regex which would be slower. 您不需要使用速度较慢的正则表达式。 Use strpos() : 使用strpos()

$status = (0 === strpos($url, 'search/site') && FALSE === strpos($url, 'ss_language'))
    ? 'PASS'
    : 'FAIL';

Check out the: DEMO 看看: DEMO

Basically what it does is it checks to see if the URL starts with search/site and if it does it then check to see if it does NOT contain ss_language and will return TRUE or FALSE or Pass/Fail in this example. 基本上它的作用是检查URL是否以search/site开头,如果是,则检查它是否不包含ss_language并在此示例中返回TRUEFALSE或Pass / Fail。 Regex is not needed at all as you are just looking for substrings. 因为您只是在寻找子串,所以根本不需要正则表达式。

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

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