Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.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_Overlapping Matches - Fatal编程技术网

Php 正则表达式查找顺序整数

Php 正则表达式查找顺序整数,php,regex,overlapping-matches,Php,Regex,Overlapping Matches,我很难让我的正则表达式代码在PHP中正常工作。这是我的密码: $array = array(); // Used to satisfy the 3rd argument requirment of preg_match_all. $regex = '/(012|345|678|987|654|321|123|456|789|876|543|210|234|567|765|432)/'; $subject = '123456'; echo preg_match_all($regex, $subje

我很难让我的正则表达式代码在PHP中正常工作。这是我的密码:

$array = array(); // Used to satisfy the 3rd argument requirment of preg_match_all.
$regex = '/(012|345|678|987|654|321|123|456|789|876|543|210|234|567|765|432)/';
$subject = '123456';
echo preg_match_all($regex, $subject, $array).'<br />';
print_r($array);
我该怎么做才能使它匹配123、234、345和456


提前谢谢

Regex不是此作业的合适工具(它不会返回“子匹配”)。只需在循环中使用
strpos

$subject = '123456';

$seqs = array('012', '345', '678', '987', '654', '321', '123', '456', '234');
foreach ($seqs as $seq) {
    if (strpos($subject, $seq) !== false) {
        // found
    }
}

这可以通过正则表达式来实现。原始代码的问题是,一旦匹配发生,字符就会被使用,正则表达式将不会回溯。这里有一种方法:

$array = array(); // Used to satisfy the 3rd argument requirment of preg_match_all.
$regex = '/012|345|678|987|654|321|123|456|789|876|543|210|234|567|765|432/';
$subject = '123456';

$tempSubject = $subject;
$finalAnswer = array();
do {
    $matched = preg_match($regex, $tempSubject, $array);
    $finalAnswer = array_merge($finalAnswer, $array);
    $tempSubject = substr($tempSubject, 1);
} while ($matched && (strlen($tempSubject >= 3)));
print_r($finalAnswer);

然而,正如另一个答案所建议的,正则表达式可能不是在这种情况下使用的正确工具,这取决于您的更大目标。此外,上述代码可能不是使用正则表达式解决此问题的最有效方法(wrt内存或wrt性能)。这只是一个严格的满足需求的解决方案。

是的,这是一个黑客程序,但你可以使用正则表达式

<?php
$subject = '123456';

$rs = findmatches($subject);
echo '<pre>'.print_r($rs,true).'</pre><br />';


function findmatches($x) {
    $regex = '/(\d{3})/'; 

    // Loop through the subject string
    for($counter =  0; $counter <= strlen($x); $counter++) {
        $y = substr($x, $counter);
        if(preg_match_all($regex, $y, $array)) {
            $rs_array[$counter] = array_unique($array);
        }
    }

    // Parse results array
    foreach($rs_array as $tmp_arr) {
        $rs[] = $tmp_arr[0][0];
    }
    return $rs;
}
?>
注意:这仅适用于并发数字

Array
(
    [0] => 123
    [1] => 234
    [2] => 345
    [3] => 456
)
输出:

Array
(
    [0] => Array
    (
        [0] => 
        [1] => 
        [2] => 
        [3] => 
    )

    [1] => Array
    (
        [0] => 123
        [1] => 234
        [2] => 345
        [3] => 456
    )
)
您试图检索主题字符串中相互重叠的匹配项,这通常是不可能的。然而,在许多情况下,您可以通过将整个正则表达式包装在捕获组中,然后将其包装在前瞻中来伪造它。因为前瞻在匹配时不消耗任何字符,所以正则表达式引擎在每次成功匹配后手动向前移动一个位置,以避免陷入无限循环。但是捕获组仍然有效,因此您可以用通常的方式检索捕获的文本

请注意,我只打印了第一个捕获组的内容(
$array[1]
)。如果我打印了整个数组(
$array
),它会是这样的:

|(管道)使正则表达式引擎匹配左侧或右侧的部分。可以串成一系列选项。
$regex = '/(?=(012|345|678|987|654|321|123|456|789|876|543|210|234|567|765|432))/';
$subject = '123456';
preg_match_all($regex, $subject, $array);
print_r($array[1]);
Array
(
    [0] => 123
    [1] => 234
    [2] => 345
    [3] => 456
)
Array
(
    [0] => Array
    (
        [0] => 
        [1] => 
        [2] => 
        [3] => 
    )

    [1] => Array
    (
        [0] => 123
        [1] => 234
        [2] => 345
        [3] => 456
    )
)