Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

php regex match a word but don't match these other ones

I'm trying to sort though a list of pipe fittings using PHP with regex, I know how to match more then one word but I can't figure out how to not match words. I need it to not match "bolts" and "nuts"(with or without the s).

Sort list simple

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

if(preg_match('/black|union/', $_POST["fitting_name$x"])){  
echo "show results";
}

Seems like I need to be looking at negative lookahead I tried .(?!bolts) also without the dot but didn't work for me. I tried a few other things but got to a point where I was just throwing things at it hoping for something to stick.

I'm really bad at regex so I may have seen the right way to do it but couldn't figure out how to make it work. Also thanks for any help you can give.

question from:https://stackoverflow.com/questions/65841598/php-regex-match-a-word-but-dont-match-these-other-ones

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use negative lookaheads:

/^(?!.*(bolt|nut)s?).*(black|union)/
/                                         : Starting delimiter
 ^                                        : Matches the start of the string
  (?!                                     : Start of negative lookahead
     .*                                   : Matches any character 0 or more times
                                        : Matches a word boundary before the target word
         (bolt|nut)                       : Literal match "bolt" OR "nut"
                   s?                     : Matches an optional "s"
                                        : 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

Using the either side of the word means that you don't accidentally filter out words which contain the word bolt|nut for example: 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('/^(?!.*(bolt|nut)s?).*(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!
*/

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...