PHP—PCRE regular expression delimiter

伊谢尔伦
Release: 2016-11-21 17:28:57
Original
1260 people have browsed it

When using the PCRE function, the pattern needs to be enclosed by delimiters. The delimiter can be any non-alphanumeric, non-backslash, or non-whitespace character.

Commonly used delimiters are forward slash (/), hash symbol (#) and negation symbol (~). The following examples are all patterns using legal delimiters.

/foo bar/
#^[^0-9]$#
+php+
%[a-zA-Z0-9_-]%
Copy after login

If a delimiter needs to be matched within a pattern, it must be escaped with a backslash. If delimiters occur frequently within the pattern, a better option is to use other delimiters to improve readability.

/http:\/\//
#http://#
Copy after login

When you need to put a string into a pattern for use, you can use the preg_quote() function to escape it. Its second parameter (optional) can be used to specify the delimiter that needs to be escaped.

In addition to the delimiters mentioned above, you can also use bracket-style delimiters. The left bracket and the right bracket serve as the start and end delimiters respectively.

{this is a pattern}
Copy after login

You can add pattern modifiers after the closing delimiter. The following example is a case-insensitive match:

#[a-z]#i
Copy after login


Related labels:
php
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!