简体   繁体   English

PHP或Regex:字符串与搜索模式中的所有字符匹配

[英]PHP or Regex: string matches ALL characters in search pattern

I need to build a regex that will look for the occurrence of all characters in an inputted string. 我需要构建一个正则表达式,以查找输入字符串中所有字符的出现。

For example, if the user inputs "equ" as the search parameter, "queen" and "obsequious" would match, but "qadaffi" and "tour" and "quail" would not. 例如,如果用户输入“ equ”作为搜索参数,则“ queen”和“ obsequious”将匹配,但是“ qadaffi”,“ tour”和“ quail”将不匹配。

Obviously I'm trying the basic /[equ]/ pattern and it's looking for "at least one of". 显然,我正在尝试基本的/ [equ //]模式,它正在寻找“至少一个”。

If there's a basic PHP function that would do this without regex, then that would be acceptable. 如果有一个基本的PHP函数无需正则表达式就可以执行此操作,那将是可以接受的。 But sad. 但是可悲。

/[equ]/ is a character class which means it matches just one character. /[equ]/是一个字符类,这意味着它仅匹配一个字符。 Try /.*equ.*/ instead. 请尝试/.*equ.*/ I haven't used the php matching functions, so the .* 's might be unnecessary. 我没有使用过php匹配功能,因此.*可能是不必要的。

Edit: Apparently they're definitely unnecessary, so just use /equ/ . 编辑:显然,它们绝对是不必要的,因此只需使用/equ/

yeah, agreed that simple for loop would be more efficient in your case. 是的,同意简单的for循环在您的情况下会更有效率。

assuming $query = "que"; 假设$query = "que"; and $input = "queen"; $input = "queen"; or anything else: 或其他:

$matched = true;
$len = strlen($query); // or mb_strlen($query) if you have multibyte string in input
for ($i = 0; $i < $len; $i++){
    if (!strstr($input, $query[$i])){
        $matched = false;
        break;
    }
}

very primitive loop to begin with. 首先是非常原始的循环。

@sln @jancha @sln @jancha

I've implemented a timer to measure the speeds. 我已经实现了一个计时器来测量速度。 Oddly, I'm finding that the regex is faster than the loop in my code. 奇怪的是,我发现正则表达式比代码中的循环快。 Is this right? 这是正确的吗?

$haystack = "Obsequious";
$needle = array('e','q','u');
$regex = "/^(?=.*e)(?=.*q)(?=.*u)/";

function trial(){
    GLOBAL $haystack;
    GLOBAL $needle;
    foreach ($needle as $n) {
        if (!strpos($haystack, $n)) return false;
    }
    return true;
}

function trial2(){
    GLOBAL $haystack;
    GLOBAL $regex;
    if (preg_match($regex, $haystack)) {
        return true;
    }
    return false;
}

print time_trial("trial");
print time_trial("trial2");

function time_trial($function, $iterations=100000){
    $before = microtime(true);
    for ($i=0 ; $i<$iterations ; $i++) { 
        call_user_func($function);
    }
    $after = microtime(true);
    $total = round($after-$before, 4);  
    return "Executed timed trial '$function' // $iterations iterations // $total seconds<br />\n";
}

Probably a regex is not the tool for this job. 正则表达式可能不是这项工作的工具。 Not a php expert, but loop through each required character checking that it exists in the object string. 不是php专家,而是遍历每个必需的字符以检查它是否存在于对象字符串中。

Otherwise, using a regex does about the same, but its slow and overkill: 否则,使用正则表达式可以达到相同的目的,但是速度慢且过大:

/^(?=.*e)(?=.*q)(?=.*u)/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM