Home > Backend Development > PHP Problem > What are regular expression delimiters? What do we need to pay attention to? (with code)

What are regular expression delimiters? What do we need to pay attention to? (with code)

慕斯
Release: 2023-03-10 12:48:01
Original
2522 people have browsed it

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! ! !

What are regular expression delimiters? What do we need to pay attention to? (with code)

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 = '';
Copy after login

Specifically, we take the code demonstration as an example:

<?php
/******正则表达式的定界符*****/
$pattern = &#39;/ /&#39;;
$str = &#39;&#39;;
//进行匹配
preg_match( $pattern, $str);
?>
Copy after login

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:

What are regular expression delimiters? What do we need to pay attention to? (with code)

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 = &#39;/ /&#39;;
$pattern = &#39;\ \&#39;;
$str = &#39;&#39;;
//进行匹配
preg_match( $pattern, $str);
?>
Copy after login

Run results:

What are regular expression delimiters? What do we need to pay attention to? (with code)

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template