Regular expression (regex) or operator "|" Bug_ Regular expressions with feature-prone features

Source: Internet
Author: User
Tags numeric

Next: Regular expression (regex) Error usage causes a feature vulnerability, we continue to comb, regular expression error use, resulting in functional design vulnerabilities (bugs), do the web, need to master a lot of knowledge, web development this work, in the domestic also more than 10 years, many developers, are through: training ( self-study)-> imitate-> do project, such a process. Many of them have changed and become web development engineers. This line of entry is low and easy to get started. But it's not easy to be a master. Need to learn, master the knowledge of dozens of doors. And for just getting started with people, many times because of lack of systematic learning, theoretical support. It's a bit of a lift. Therefore, the emergence of these more or less functional design vulnerabilities, is very common!

Well, some digress, in the code to go to check, such vulnerabilities are often present. Let's look at the following code:

<?php
$user = "bcd123 John";
Match the user name in the beginning of ABC and BCD, followed by the numeric character
 
Preg_match_all ("/abc|bcd\d+/", $user, $match);
Var_dump ($match);
 
/* result is
Array (1) {
 [0]=>
 Array (1) {
 [0]=>
 string (6) ' bcd123 '
 }
}}

Finds all pages appear with ABC or BCD beginning, followed by a numeric string. Through the above, we see that the correct match to bcd123, if we enter: $user = "abc123 John", the discovery cannot match. The reason is "|" Character, the lowest priority, the above writing will be: matching ABC or bcd\d+ string.

The above diagram, which will | be included in the ().

If you want to raise the priority, you can (ABC|BCD) \d+, match all the ABC or BCD strings, and follow the numbers immediately. In the use of "|" String, note that it has a low priority level and can be placed in () if you want to match the priority.

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.