The doubts of the regular expressions W and D

Source: Internet
Author: User
Keywords Php
$string='April15,2003';$pattern='/(\w+)(\d+),(\d+)/i';$replacement='${1},${3}';echo preg_replace($pattern,$replacement,$string);

?>
Results for April1, 2003
Why (w+) matches April 1 without matching April 15
Is it because: W itself will match the number, if the 5 is also matched, then D there is no match to the thing

Reply content:

$string='April15,2003';$pattern='/(\w+)(\d+),(\d+)/i';$replacement='${1},${3}';echo preg_replace($pattern,$replacement,$string);

?>
Results for April1, 2003
Why (w+) matches April 1 without matching April 15
Is it because: W itself will match the number, if the 5 is also matched, then D there is no match to the thing

w+ can match April15 completely, but you wrote d+ behind w+, in order to match the rules you defined, April1 gave w,5 to D.
$pattern='([a-z]+)(\d+),(\d{4})/i';
is in line with the requirements

$pattern = '/(w+) (d{1,2{+), (d+)/I '; this looks like the landlord wants the date

  • 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.