Two universal principles of regular expressions
/** Rule: first select the leftmost matching result * example: use/fat | cat | belly | your/to match the character 'The dragging belly indicated that your cat is too fat. 'What is the result? * Result: it is belly *. Although fat is at the top of all possible options, it is not the first matching result, so it is not selected, other possibilities will also be matched */preg_match ('/fat | cat | belly | your/', 'The dragging belly indicated that your cat is too fat. ', $ B); // If you use the preg_match_all () function, you can match the situation. why is it possible that greedy matching is inefficient? // var_dump ($ B ); /** rule: the standard quantifiers are matched first * Example: use subject :(. *) to match 'Subject: wyxwuyongxin@126.com '* Change 1: subject :(. *). ** Change 2: subject :(. *)(. *) * result: wyxwuyongxin@26.com * Change 1 Result: subject: wyxwuyongxin@26.com wyxwuyongxin@26.com * Change 2 result: subject: wyxwuyongxin@26.com $1 wyxwuyongxin@26.com $2 is EMPTY * Change 1 cause :. * is preferentially matched and can capture the value of $1 as the first wyxwuyongxin@26.com. * The text of the entire title is occupied, instead of the second one. * any * what character * The second character cannot be added because. * The operation can be successful without matching any characters. * cause 2: The service is started first, and no matching characters must be followed. Therefore, some characters in the previous priority section are not forced to be released */preg_match ('/subject :(. *)/', 'Subject: wyxwuyongxin@26.com', $ c); preg_match ('/subject :(. *). */', 'Subject: wyxwuyongxin@26.com', $ c); preg_match ('/subject :(. *)(. *)/', 'Subject: wyxwuyongxin@26.com', $ c); // var_dump ($ c);/** example: use/^. * ([0-9] [0-9])/matching about 24 long * result: about 24 $1 capture 24 * cause: (excessive priority) [0-9] indicates that the row must be matched. if it fails to be matched at the end of the row, a notification will be sent. * "You have occupied too much space and handed it over, so that I can match it." The priority setting first matches as many characters as possible, but for the whole expression, he usually needs to release some characters. when the first [0-9] requires the release of 'G', but * still cannot match, he must return the next n o l until the second [0-9] of '4- 9] if you want to release a character, you will get $1 24 */preg_match ('/^. * ([0-9] [0-9])/', 'About 24 long', $ d); // var_dump ($ d);/** USE /. * [0-9] +/to match 'copyright 2003. '* result: copyright 2003 $1 capture 3 * cause: after 3 and are released, 3 can be matched by [0-9], but [0-9] is modified by +, and as many matches as possible are encountered '. 'The other matching characters cannot be found * no matching element is required, so 0 is not forced to be handed over (first come first served principle). Therefore, the final value of $1 is 3 */preg_match ('/. * ([0-9] +)/', 'copyright 2003. ', $ str); var_dump ($ str );