1. A brief introduction to the need to find strings that meet certain complex rules when writing programs or webpages that process strings. Regular Expressions are syntaxes used to describe these rules. For example, when determining the user's email address format, mobile phone number format, or collecting others' webpage content, the main functions are: segmentation, matching, search, and replacement. Note: Regular Expression Pairs
1. A brief introduction to the need to find strings that meet certain complex rules when writing programs or webpages that process strings. Regular Expressions are syntaxes used to describe these rules. For example, when determining the user's email address format, mobile phone number format, or collecting others' webpage content, the main functions are: segmentation, matching, search, and replacement. Note: Regular Expression Pairs
1. A brief introduction to the need to find strings that meet certain complex rules when writing programs or webpages that process strings. Regular Expressions are syntaxes used to describe these rules. For example, when determining the user's email address format, mobile phone number format, or collecting others' webpage content, the main functions are: segmentation, matching, search, and replacement. Note: regular Expressions are a critical knowledge point for a programmer. Therefore, learning regular expressions is essential for every programmer. It not only helps us do some work that cannot be implemented through functions, but also helps us reduce a lot of work. 2. Two common regular functions in PHP: preg_match, preg_match (mode, string subject, array matches), ereg, and POSIX (Unix and Script) ereg (mode, string subject, array regs) 3. Elements (1) and atoms (common characters: a-z A-Z 0-9, atomic table, escape characters) (2), metacharacters (with special features) (3), pattern modifier (some built-in characters I, m, S, U ...) 4. In the regular expression, the "atom" ① a-z A-Z _ 0-9 // The most common character ② (abc) (skd) // The units contained in parentheses conform to ③ [abcs] [^ abd] // The Atomic table contained in square brackets, ^ in the atomic table indicates exclusion or inverse content ④ Escape Character \ d contains all numbers [0-9] \ D except all numbers [^ 0-9] \ w contains all English characters [a-zA-Z_0-9] \ W except all English characters [^ a-zA-Z_0-9] \ s contains blank areas such as carriage return, line feed, paging, etc. [\ f \ n \ r] 5, regular expression metacharacters * match the previous content 0 times once or multiple times. match content 0 times 1 time or multiple times, but does not contain carriage return line breaks + match the previous content 1 time or multiple times? Match the previous content 0 times or 1 time | select to match something similar to PHP | (because this operation conforms to the weak type, leading to the most overall match) ^ match string header content $ match string tail content \ B match word boundary, the boundary can be a space or a special match. \ B match. Except for the unexpected content with the word boundary, {m} matches the previous content. The number of repetitions is M. {m ,} the number of repetitions matching the previous content is greater than or equal to M {m, n}. The number of repetitions matching the previous content is M to N (). The overall match is merged and put into the memory, \ 1 \ 2... 6. The order of operation is still in the order of priority (). Since the round brackets are processed in memory, the priority is the highest *? + {} Repeat Matching content followed by ③ ^ $ \ B boundary processing 3④ | condition processing 4th Finally, matching is calculated in order of Operation 7. Pattern modifier, is a function enhanced and supplemented for regular expressions. The common modifier I regular content is not case sensitive during matching (the default value is case sensitive) m. When matching the first or last content, use multiple lines to identify and match. S. The Escape carriage return is canceled as a single line match. when matching, x ignores the blank space in the regular expression. A is forced to match D from the beginning. Force $ NO content at the end of the match. \ nU prohibits greedy matching. Only the latest matching character is tracked and the specified tag is ended. yes, the tag pair can contain the following content:/<\ s * h4 [^>] *> (. *?) <\ S */\ s * h4>/g matches all tags and tag attributes:/<(. | \ n) *?> /G matches all start tags. The tag can contain the following attributes:/<\ s * \ w. *?> /G matches all end tags:/<\ s * \/\ s * \ w \ s *. *?> | <\ S * br \ s *>/g matches the specified start tag. The tag can contain the following attributes:/<\ s * p. *?> /G match the end tag with content between tag pairs:/<\ s * \/\ s * p \ s *. *?> /G matches all specified labels (no matter the start or end tags). The label can have the following attributes:/<\ s *\/? \ S * span \ s *. *?> /G match the start tag with the specified attribute:/<\ s * \ w * \ s * style. *?> /G matches the start tag with the specified attribute and the specified attribute value:/<\ s * \ w * \ s * href \ s * = \ s *"? \ S * ([\ w \ s % # \/\.;: _-] *) \ s *"?. *?> /G
Original article address: simple notes for PHP regular expressions. Thank you for sharing them with me.