Regular Expression (regex) or operator "|" It is easy to use Function Bug, regexbug

Source: Internet
Author: User

Regular Expression (regex) or operator "|" It is easy to use Function Bug, regexbug

Last article: Regular Expressions (regex) are incorrectly used, leading to function vulnerabilities. We will continue to sort out this article. Regular Expressions are incorrectly used, leading to functional design vulnerabilities (bugs) and web applications, A lot of knowledge is required. The website development work has been in China for more than 10 years. Many developers use the process of training (self-learning)-> imitating-> Project Creation. Many of them become web development engineers after repair and modification. This line is easy to get started. However, it is not easy to become a master. You need to learn and master dozens of courses. For beginners, many people often lack systematic learning and theoretical support. As a result, there is a lack of effort to improve! Therefore, these more or less functional design vulnerabilities are very common!

Well, there are some problems. This type of vulnerability often occurs during code lookup. Let's take a look at the following code:

<? Php $ user = "bcd123 zhangsan"; // The matched username starts with abc and bcd, followed by the digit preg_match_all ("/abc | bcd \ d + /", $ user, $ match); var_dump ($ match);/* The result is array (1) {[0] => array (1) {[0] => string (6) "bcd123 "}}*/

Search for all pages that start with abc or bcd, followed by a numeric string. Through the above, we can see that bcd123 is correctly matched. If we enter $ user = "abc123 zhangsan", we will find that it cannot be matched. The reason is that the character "|" has the lowest priority. The above writing method will be: Match abc or bcd \ d + String.

To include | in.

To increase the priority, you can (abc | bcd) \ d + to match all abc or bcd strings followed by digits. When using the "|" string, note that its priority level is low. If you want to prioritize matching, you can put it in.

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.