Note 014 Delimiter for PHP regular expressions

黄舟
Release: 2023-03-04 09:14:01
Original
1681 people have browsed it

is written in front

The PHP regular delimiter used by Hy369 is slash (/, forward slash). I even thought that this was the only regular delimiter in PHP.

Hey, until today, I saw a rather strange example of using the # sign as a regular separator. Seeing this "strange" regular expression, I couldn't help but ask Du Niang. The result: I was stunned.

Really, until this moment, I realized that the regular separator under PHP is not actually only / .

Okay, let’s record this knowledge point that surprised Hy369.

PHP Regular expression delimiter

In PHP, the regular expression delimiter can be any character except: letters, numbers, backslashes, and whitespace characters.

Commonly used delimiters are: slash (/, forward slash), pound sign (#, hash sign) and tilde (~, tilde).

In other words, it can be the following patterns:

/test string/
#^[^0-9]Hy369$#
+php blog+
%[A-Za-z0-9_-]%
Copy after login

Key points

If in a certain pattern, the delimiter in this pattern needs to be used in the regular expression , you need to add a backslash before this separator. For example:

/http:\/\//
Copy after login

In this case, the entire regular expression is not very intuitive. At this time, you can consider changing a delimiter to enhance the readability of the regular expression. Therefore, the above regular expression can be replaced by this:

#http://#
Copy after login

The above is the content of note 014 PHP regular expression delimiter. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!