我试图通过使用带有正则表达式的PHP的管道配件列表进行排序,我知道如何匹配多个单词,但我不知道如何不匹配单词。我需要它不匹配“螺栓”和“螺母”(带有或不带有s)。
排序列表简单
0-2“ x6”黑色乳头
0-1 / 2x4黑色乳头
20-3/4“ x 3/8”黑色衬套。
10-3/4“黑色插头螺纹
0-7/8 x 3 3/4黑色螺栓
0 -7/8黑色螺母
if(preg_match('/black|union/', $_POST["fitting_name$x"])){
echo "show results";
}
似乎我需要先看消极的前瞻,我.(?!bolts)
也尝试了没有圆点但对我不起作用。我尝试了其他一些操作,但是到了要点,我只是在扔东西,希望能粘贴一些东西。
我真的对正则表达式不好,所以我可能已经找到了正确的方法,但是却不知道如何使它正常工作。也感谢您提供的任何帮助。
您可以使用否定的前瞻:
/^(?!.*\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
单词的任一侧意味着您不会意外地滤除包含该单词的单词bolt|nut
,例如:bolted flange
。
$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!
*/
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句