Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 在正则表达式中具有受控多次发生的一组模式中的模式白名单_Regex_String - Fatal编程技术网

Regex 在正则表达式中具有受控多次发生的一组模式中的模式白名单

Regex 在正则表达式中具有受控多次发生的一组模式中的模式白名单,regex,string,Regex,String,我希望以下示例返回匹配 我喜欢foobar.com 我喜欢google.com和foobar.com 我喜欢foobar.com和google.com 我喜欢foobargoogle.com和谷歌foobar.com 我喜欢yahoo.com和foobar.com 我喜欢foobar.com和yahoo.com 我喜欢foobaryahoo.com和yahoofoobar.com 我不希望以下示例返回匹配 我喜欢雅虎网站 我喜欢foobaryahoo.com 我喜欢google.com 我

我希望以下示例返回匹配

  • 我喜欢foobar.com
  • 我喜欢google.com和foobar.com
  • 我喜欢foobar.com和google.com
  • 我喜欢foobargoogle.com和谷歌foobar.com
  • 我喜欢yahoo.com和foobar.com
  • 我喜欢foobar.com和yahoo.com
  • 我喜欢foobaryahoo.com和yahoofoobar.com
我不希望以下示例返回匹配

  • 我喜欢雅虎网站
  • 我喜欢foobaryahoo.com
  • 我喜欢google.com
  • 我喜欢foobargoogle.com
  • 我喜欢google.comyahoo.com
  • 我喜欢foobargoogle.com和foobaryahoo.com
注意-它不是相等匹配,而是包含匹配

我尝试了以下正则表达式模式:

(?!(^.*((google)|(yahoo))\.com.*$))(^.*\w+\.com.*$)
但一旦“google.com”或“yahoo.com”出现,即使“foobar.com”出现在它之前,它也没有匹配的终止

我喜欢foobar.com,但不喜欢google.com

基本上,我希望它忽略字符串中的“google.com”和“yahoo.com”,并检测任何其他类型:“\w+.com”

注:

  • google.comyahoo.com只是例子。所以,它应该忽略任何字符串长度的字母数字字符的匹配集
  • 忽略大小写和空格

您可以通过PCRE动词来实现这一点

说明:

  • (?:google | yahoo)(?:\.com)
    突然将
    google
    yahoo
    字符串与以下可选的
    .com
    匹配。请看演示

  • (?:google | yahoo)(?:\.com)(*SKIP)(*F)
    和以下PCRE动词
    (*SKIP)(*F)
    将导致上一次匹配失败。下面的
    |
    或将匹配所有未后跟
    google
    yahoo
    字符串的边界。请看演示

  • < > >代码>((:谷歌雅虎)\W)+\COM现在ReGEX引擎将考虑匹配边界作为起点,它突然匹配一个或多个单词字符,但不符合<代码>谷歌< /代码>或<代码>雅虎< /C> >

  • \.com
    仅帮助匹配以
    .com
    结尾的字符串

    • 没必要这么复杂。
      这将准确地查找您在“需要”行中突出显示的内容

      (?:\w(?)?
      (?:google|yahoo)(?:\.com)?(*SKIP)(*F)|(?:(?!google|yahoo)\w)+\.com
      ^                                    ^                           ^
      |------Part you don't want-----------|------------Part you want--|
      
       # (?:\w(?<!google)(?<!yahoo))+\.com
      
       (?:
            \w                # Match words but not google or yahoo behind
            (?<! google )
            (?<! yahoo )
       )+
       \. com