How to create a regex that will match regardless of the sequence of characters?

Say I am requiring a user to input the certain words provided and he/will just input it on a textbox which will be validated regardless of order.I have been looking for answers for hours and I'm stuck :/

eg the words should be inputted are: foo, bar, green.

and I can still match it even if the order is bar green foo or green foo bar

I pretty much know the very basics of regex but I don't know how to match with this type of condition.. Any answers will be much appreciated!


这会检查输入中是否有这三个单词,而不是更多:

/^(?=.*bbarb)(?=.*bgreenb)(?=.*bfoob)(?:s*bw+bs*){3}$/s

simple answer: don't use regex.

Just explode() the input, and then check manually if the words are ok. A simple way to check if the input is good is this:

if(!array_diff(explode(' ', $input), $valid_words)) {
    // input OK
}

use explode()

http://sandbox.phpcode.eu/g/59c57

<?php 
$str = 'foo, bar, green'; 
$array = explode(',', $str); 
print_r($array);
链接地址: http://www.djcxy.com/p/13448.html

上一篇: 正则表达式匹配的东西,如果它没有其他的东西

下一篇: 无论字符序列如何创建匹配的正则表达式?