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