I am a programmer and a beginner in PHP. I have encountered a regular expression matching problem, so I am eager to explain it. My code is as follows: & lt ;! DOCTYPE & nbsp; HTML & gt; & lt; html & nbsp; lang = "zh-CN" & g tutorial on PHP regular expression matching
I am a programmer and a beginner in PHP. I have encountered a regular expression matching problem, which is hard to understand. I 'd like to ask for advice.
My code is as follows:
First PHP webpage
$str = "akk c";
$pattern="/(.)\1/";
echo preg_match($pattern,$str,$matches)."
";
var_dump($matches);
?>
The output result is:
0
Array (size = 0)
Empty
Thank you!
In addition, my environment configuration is:
Server version: 5.6.12-log-MySQL Community Server (GPL)
Apache/2.4.4 (Win64) PHP/5.4.12
Php, regular expressions shared: ... 'Data-pics = ''>
------ Solution --------------------
If you want to match kk
$str = "akk c";
$pattern="/k+/";
echo preg_match($pattern,$str,$matches)."
";
var_dump($matches);
------ Solution --------------------
Or
$str = "akk c";
$pattern="/(.)\\1/";
echo preg_match($pattern,$str,$matches)."
";
var_dump($matches);
------ Solution --------------------
It is best to use single quotation marks when writing regular expressions. double quotation marks will handle a lot of escape operations, so you need to add a lot \
For example, \ 1 is regarded as \ x01 and must be written as \ 1 to be processed correctly.