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

WBOY
Release: 2016-08-08 09:31:29
Original
1059 people have browsed it

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;
Copy after login

The result is:

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

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);
Copy after login

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

Array
(
    [0] => push_1113_20150105000052430.txt
)
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template