Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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
php正则表达式匹配一个单词,但不匹配';我和其他的不相配_Php_Regex - Fatal编程技术网

php正则表达式匹配一个单词,但不匹配';我和其他的不相配

php正则表达式匹配一个单词,但不匹配';我和其他的不相配,php,regex,Php,Regex,我正在尝试使用PHP和正则表达式对管件列表进行排序,我知道如何匹配多个单词,但我不知道如何不匹配单词。我需要它不匹配“螺栓”和“螺母”(带或不带s) 简单排序列表 0-2“x6”黑色螺纹接头 0-1/2x4黑色螺纹接头 20-3/4英寸x 3/8英寸黑色衬套 10-3/4“黑色螺纹塞 0-7/8 x 3/4黑色螺栓 0-7/8黑色螺母 if(preg_match('/black|union/', $_POST["fitting_name$x"])){ echo "

我正在尝试使用PHP和正则表达式对管件列表进行排序,我知道如何匹配多个单词,但我不知道如何不匹配单词。我需要它不匹配“螺栓”和“螺母”(带或不带s)

简单排序列表

0-2“x6”黑色螺纹接头

0-1/2x4黑色螺纹接头

20-3/4英寸x 3/8英寸黑色衬套

10-3/4“黑色螺纹塞

0-7/8 x 3/4黑色螺栓

0-7/8黑色螺母

if(preg_match('/black|union/', $_POST["fitting_name$x"])){  
echo "show results";
}
似乎我需要看消极的前瞻性。我尝试了
(!bolts)
也没有点,但对我不起作用。我尝试了一些其他的东西,但到了一个地步,我只是朝它扔东西,希望有东西粘在上面


我在ReGEX上真的很差,所以我可能已经看到了正确的方法去做,但是不知道如何使它有效。也谢谢你提供的任何帮助。

< P>你可以考虑使用类似

的模式。
/\b(?:黑色|活接头?\b(?。*\b(?:螺栓|螺母)s?\b)/
如果需要,在最后一个
/
之后添加
i
,使其不区分大小写。请参阅

详情:

  • \b
    -单词边界
  • (?:黑色|联合体?
    -
    黑色
    联合体
    联合体
  • \b
    -单词边界
  • (?!..\b(?:螺栓|螺母)s?\b)
    -如果在当前位置的右侧,有
    • *
      -尽可能多的零个或多个字符(断线字符除外)
    • \b(?:螺栓|螺母)s?\b
      -
      螺栓
      /
      螺栓
      螺母
      /
      螺母
      作为一个整体

可以考虑使用类似

的模式。
/\b(?:黑色|活接头?\b(?。*\b(?:螺栓|螺母)s?\b)/
如果需要,在最后一个
/
之后添加
i
,使其不区分大小写。请参阅

详情:

  • \b
    -单词边界
  • (?:黑色|联合体?
    -
    黑色
    联合体
    联合体
  • \b
    -单词边界
  • (?!..\b(?:螺栓|螺母)s?\b)
    -如果在当前位置的右侧,有
    • *
      -尽可能多的零个或多个字符(断线字符除外)
    • \b(?:螺栓|螺母)s?\b
      -
      螺栓
      /
      螺栓
      螺母
      /
      螺母
      作为一个整体

    • 这种模式应该有效:
      螺栓?\b螺母?\b.*$
      (我用
      螺栓
      替换了
      螺栓

      它实际上使用了负前瞻,但考虑到在单词
      bolt
      nut
      之前可能会有更多的字母
      *
      s?
      来考虑使用0或1次的复数形式,
      \b
      来匹配单词的结尾,这样单词
      nuts
      仍然会匹配。

      这样的模式n应该起作用:
      ^(?。*bolts?\b |.*nuts?\b)。*$
      (我用
      螺栓
      替换了
      螺栓

      它实际上使用了负前瞻,但考虑到在单词
      bolt
      nut
      之前可能会有更多的字母
      *
      s?
      来考虑使用0或1次的复数形式,
      \b
      来匹配单词的结尾,这样单词
      nuts
      仍然会匹配。

      您可以使用负面表情头:

      /^(?!.*\b(bolt|nut)s?\b).*(black|union)/
      /                                         : Starting delimiter
       ^                                        : Matches the start of the string
        (?!                                     : Start of negative lookahead
           .*                                   : Matches any character 0 or more times
             \b                                 : Matches a word boundary before the target word
               (bolt|nut)                       : Literal match "bolt" OR "nut"
                         s?                     : Matches an optional "s"
                           \b                   : Matches a word boundary after the target word
                             )                  : End of negative lookahead
                              .*                : Match any charachter 0 or more times
                                (black|union)   : Literal match "black" OR "union"
                                             /  : Ending delimiter
      
      使用
      \b
      这个词的任何一侧都意味着您不会意外地过滤掉包含
      螺栓|螺母
      这个词的词,例如:
      螺栓法兰

      $stringList = [
          '0 - 2"x6" black nipple',
          '0 - 1/2x4 black nipple',
          '20 - 3/4" x 3/8" black bushing.',
          '10 - 3/4" black plugs thread',
          '0 - 7/8 x 3 3/4 black bolts',
          '0 -7/8 black nuts'
      ];
      
      foreach($stringList as $string){
          var_dump(
              preg_match('/^(?!.*\b(bolt|nut)s?\b).*(black|union)/', $string)
          );
      }
      
      /* Output...
      
      int(1)
      int(1)
      int(1)
      int(1)
      int(0)
      int(0)
      
      i.e. matches for all but the last 2!
      */
      

      您可以使用负外观标题:

      /^(?!.*\b(bolt|nut)s?\b).*(black|union)/
      /                                         : Starting delimiter
       ^                                        : Matches the start of the string
        (?!                                     : Start of negative lookahead
           .*                                   : Matches any character 0 or more times
             \b                                 : Matches a word boundary before the target word
               (bolt|nut)                       : Literal match "bolt" OR "nut"
                         s?                     : Matches an optional "s"
                           \b                   : Matches a word boundary after the target word
                             )                  : End of negative lookahead
                              .*                : Match any charachter 0 or more times
                                (black|union)   : Literal match "black" OR "union"
                                             /  : Ending delimiter
      
      使用
      \b
      这个词的任何一侧都意味着您不会意外地过滤掉包含
      螺栓|螺母
      这个词的词,例如:
      螺栓法兰

      $stringList = [
          '0 - 2"x6" black nipple',
          '0 - 1/2x4 black nipple',
          '20 - 3/4" x 3/8" black bushing.',
          '10 - 3/4" black plugs thread',
          '0 - 7/8 x 3 3/4 black bolts',
          '0 -7/8 black nuts'
      ];
      
      foreach($stringList as $string){
          var_dump(
              preg_match('/^(?!.*\b(bolt|nut)s?\b).*(black|union)/', $string)
          );
      }
      
      /* Output...
      
      int(1)
      int(1)
      int(1)
      int(1)
      int(0)
      int(0)
      
      i.e. matches for all but the last 2!
      */
      

      你的评论对理解每件事的工作原理有很大帮助。为此我非常感谢你。看来你自己也对管道安装有点了解。因为我从来没有说过“螺栓法兰”“什么会给我带来麻烦呢?”斯纳克兹梅纳德不用担心,很高兴听到这个解释帮助你理解。哈哈,是的,我确实知道一点;没想到这在这里会有帮助!!你的评论对理解每件事的运作有很大帮助。为此我非常感谢你。看来你对自己的管道安装也有点了解。因为我从来没有想过“螺栓法兰”会给我带来什么问题。@SnakZMaynard不用担心,很高兴听到这个解释有助于你的理解。哈哈,是的,我确实知道一点;没想到这在这里会有帮助!!