Using character sets can immediately give regular expressions that are more powerful than exact matching. A character set can be used to match any character of a specific type; in fact, it is a wildcard character. A complete regular expression consists of two parts, metacharacters and text characters. Metacharacters are characters with special meanings, such as "*" and "?". Text characters are ordinary text, such as letters and numbers. PCRE-style regular expressions are generally placed in the middle of the delimiter "/". Next we will introduce the characters in regular expressions.
Row locators (^ and $)
Row locators are used to describe the boundaries of strings. "^" represents the beginning of the line; "$" represents the end of the line. For example:
^tm
This expression indicates that the starting position of the string I to be matched is the beginning of the line. For example: tm equal Tomorrow Moon can match, but Tomorrow Moon equal tm cannot match .But if you use:
tm$
, the latter can match but the former cannot. If the string you want to match can appear in any part of the string, you can write it directly:
tm
so that both can match.
Word delimiters (\b, \B)
Continuing the above example, use tm to match any position that appears in the string. Then similar to html, the I in utmost will also be found. But now what needs to be matched is the word tm, not part of the word. At this time, you can use the word delimiter \b, which means that the string to be searched is a complete word, such as:
\btm\b
There is also a capital \B, which means the same as \bOn the contrary. The string it matches cannot be a complete word, but a part of other words or strings. Such as:
\Btm\B
The above is the detailed content of Example usage of line locators and word delimiters (regular expression character set 1). For more information, please follow other related articles on the PHP Chinese website!