PHP matches regular expressions of consecutive numbers or letters. For more information, see. Regular expression writing rules: "/rules must be written in the middle of two slashes /".
(.: Decimal point) is used to match all characters except line breaks.
(\ S: lower-case backslash (s) is used to match a single space character, including the tab key and line break;
(\ S: uppercase backslash (S) is used to match all characters except a single space character;
(\ D: Backslash d) is used to match numbers from 0 to 9. It can also be written as follows: [0-9]
(\ W: lower-case backslash (w) is used to match letters, numbers, or underscores;
(\ W: uppercase backslash (W) is used to match all characters that do not match \ w;
Metacharacters include: + ,*,?
Metacharacters are easy to understand, so I made code results later.
The "+" metacharacter specifies that the leading character must appear one or more times consecutively = for example,/es +/matches the string "tesseessssseast12354haeasashaha". First, it must match the first letter "e, then, matching s and s must appear once or multiple times. please refer to the instance. The "*" metacharacter specifies that the leading character must appear zero or multiple times in a row = for example,/es */matches the "tesseessssseast12354haeasashaha" string, first matches the first letter e, the following s appears zero times or consecutively. check the instance. "?" Metacharacter specifies that the leading object must appear for zero consecutive times or Once = for example,/es? /Matches the "tesseessssseast12354haeasashaha" string. First, it matches with the first letter e, and the second after it appears zero or at most once (that is, the last letter s does not appear repeatedly ).
Sample code:
$str = "tesseessssseast12354haeasashaha"; echo "=====".$str." "; echo "/es+/:".preg_replace("/es+/","-\\0-",$str)." "; echo "/es*/:".preg_replace("/es*/","-\\0-",$str)." "; echo "/es?/:".preg_replace("/es?/","-\\0-",$str)." "; ?>
Execution result:
In fact, if you read it carefully, you will find that the regular expression is quite simple, unless it is a problem with the tutorial I wrote.
For more articles related to matching regular expressions of consecutive numbers or letters in PHP, refer to PHP!