PHP function Preg_match_all Simple application of regular Expressions _php tutorial

Source: Internet
Author: User
Before you know the regular expression, you need to master some of the basic knowledge of regular expressions, these if you can remember the best to remember, remember the need to use when you can find the line, just a few special characters, so that the regular expression play is special, you can see more detailed description.

The Preg_match_all function specifically explains that you can view the PHP manual, which uses preg_match_all to test the effect of regular expression.

Instance Code :

$html = ' php100php1002php1003 ';

Instance requirements: Separate the ID and contents of each div element, such as biuuu,biuuu_2,biuuu_3,php100,php1002 and php1003 (this is how some common methods of grasping are matched)

Analysis : string is a simple HTML element, each DIV element pair should be an ID and content, and is independent, first consider how to take out a div inside the ID value and content, such as: php100, and then match other similar elements. A DIV needs to take out two values, that is, two matching expressions, the first expression is used to match the ID value (BIUUU), the second expression is used to match the contents of the ID (php100), and the expressions used in regular expressions use parentheses, then the preceding elements will become the following:

(php100) (Expression 2)

OK, using the parentheses to divide the areas that need to be matched, and then how to match the content within each expression, we suspect that an ID might be a letter, a number, or an underscore, and that's a simple thing to do with the brackets, as follows:

expression 1: [a-za-z0-9_]+ (indicates matching case letters, numbers, and underscores)

How to match the expression 2, because the content of the ID can be any character, but note that the character cannot be matched <或> , because if the match between the two characters will be used to match the Div, so need to exclude the beginning of the two characters of the element, that is, do not match the <或> characters, as follows:

expression 2: [^<>]+ (denotes mismatched <和> characters)

In this way, the sub-expressions that need to be matched are implemented, but you also need to match a

http://www.bkjia.com/PHPjc/446628.html www.bkjia.com true http://www.bkjia.com/PHPjc/446628.html techarticle before you know regular expressions, you need to master some of the basics of regular expressions, which you can remember best if you remember, you can't remember when you need them, just a few special ...

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