Home >Backend Development >PHP Tutorial >Regarding the problem of duplicate items caused by regular expression matching

Regarding the problem of duplicate items caused by regular expression matching

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-08-08 09:31:291101browse

I encountered such a situation to match, but every time it matched the beginning of a duplicate:

$file = 'push_1113_20150105000052430.txt';
$file2 = 'confirm_push_1114_20150105000052430.txt';

preg_match('/^(get|push|admin).*.txt/', $file, $rs);
preg_match('/^confirm.*.txt/', $file2, $rs2);

echo '<pre class="brush:php;toolbar:false">';print_r($rs);
echo '<pre class="brush:php;toolbar:false">';print_r($rs2);

exit;

The result is:

Array
(
    [0] => push_1113_20150105000052430.txt
    [1] => push
)
<pre class="brush:php;toolbar:false">Array
(
    [0] => confirm_push_1114_20150105000052430.txt
)

You can see that the first result has duplicate matches to two push , what's going on? ?

I don’t know how it happened. I tried various things and then changed it to this:

preg_match('/^[get|push|admin].*.txt/', $file, $rs);

That is, I changed the small brackets into square brackets. The result is:

Array
(
    [0] => push_1113_20150105000052430.txt
)

The reason needs further investigation and is currently unknown, but it must be caused by the difference between () and [].

The above has introduced the problem of duplicate items caused by regular expression matching, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn