Ask for PHP regular match

Source: Internet
Author: User
PHP, regular expressions

You prawns, I am a programming enthusiast, beginner PHP, encountered a regular expression matching problem, baffled, special to ask for advice.
My code is as follows:




<title>The first page of PHP</title>


  
$str = "Akk C";
$pattern = "/(.) \1/";
Echo Preg_match ($pattern, $str, $matches). "
";
Var_dump ($matches);
?>




It is supposed to be able to match "KK", but the output is:
0
Array (size=0)
Empty

Thank you!

Another: My environment configuration is:
Server version: 5.6.12-log-mysql Community server (GPL)
apache/2.4.4 (Win64) php/5.4.12


Reply to discussion (solution)

If you want to match KK,

$str = "Akk c"; $pattern = "/k+/"; Echo Preg_match ($pattern, $str, $matches). "
"; Var_dump ($matches);

Or so

$str = "Akk c"; $pattern = "/(.) \\1/"; Echo Preg_match ($pattern, $str, $matches)."
"; Var_dump ($matches);

Thank you for your guidance. I forgot to state that I was trying to test the matching function of the regular "\num".

Thanks a lot! I can do it!

It is best to use single quotes when writing regular, and double quotes to handle many escapes, so add a lot of \
For example, \1 will be considered \x01 and must be written \\1 to handle it correctly.

Yes, I appreciate that, thank you.

Upstairs teachers: Sorry, I am new, do not know the "checkout to points" This procedure, not timely performance, please forgive, thank you again!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.