The previous article introduced you to "What is a regular expression? His usage? What are his characteristics? (With detailed explanation) ", this article continues to introduce to you what is the delimiter of regular expressions? What do we need to pay attention to? (Code attached), hurry up and learn! ! !
Regular expression delimiter:
Regular expressions are used to declare regular expressions Symbol of expression boundary, regular expression is of string type. Therefore, when defining a regular expression, first define the string type;
The boundary character "/" commonly used in regular expressions, in fact, letters and non-numeric characters as well as characters other than "V" are Can be used as a boundary character for regular expressions.
Note:
A complete regular expression has exactly two boundary characters.
The first one is the quotation mark boundary character of string type
The second one is the boundary character of regular expression
例如:$parrten = '';
Specifically, we take the code demonstration as an example:
<?php /******正则表达式的定界符*****/ $pattern = '/ /'; $str = ''; //进行匹配 preg_match( $pattern, $str); ?>
Code analysis:
First we define a variable $pattern. If the string written in the code is not used with a function, then it is an ordinary string, so we need to match. The matching function we need is (preg_match). After defining it, we need to give two Parameters, we need to match str, and then we run it. If preg_match reports an error, it means that what we just defined cannot be used as a delimiter; our running results found that there is no error (the running results are as follows), that is to say, What we just defined can be used as the delimiter of a regular expression;
The running result is:
For example, We convert the $pattern = '/ /' we just defined into a backslash\\. Will it still run successfully? When we run the results, we will find that an error will be reported. In other words, we cannot use backslashes as regular expressions. formula delimiter; because it is meaningful in itself, he is the escape character we often say, (the delimiter cannot be alphanumeric or backslash)
<?php /******正则表达式的定界符*****/ //$pattern = '/ /'; $pattern = '\ \'; $str = ''; //进行匹配 preg_match( $pattern, $str); ?>
Run results:
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What are regular expression delimiters? What do we need to pay attention to? (with code). For more information, please follow other related articles on the PHP Chinese website!