Home  >  Article  >  Backend Development  >  How to write regular expression in php

How to write regular expression in php

王林
王林Original
2019-09-12 17:45:103919browse

How to write regular expression in php

Regular expressions are a very commonly used and powerful technology in work, but they are a bit boring to learn. Today, let me tell you how to write regular expressions in php?

Delimiter

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

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

/foo bar/<br>#^[^0-9]$<br># php <br>%[a-zA-Z0-9_-]%<br>

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:\/\//<br>#http://

#When you need to put a string into a pattern for use, you can use 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}<br>

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

#[a-z]#i<br>

## escape Sequence <span style="font-size: 20px;"></span>

The backslash can be used in many ways. First, if it is followed by a non-alphanumeric character, it cancels the special meaning represented by that character. This use of the backslash as an escape character is available both inside and outside character classes.

For example, if you want to match a "*" character, you need to write "\*" in the pattern. This applies when a character would have a special meaning without escaping. However, for non-alphanumeric characters, it is always safe to add a backslash in front of it to declare that it represents itself when it needs to be matched in the original text. If you want to match a backslash, use "\\" in the pattern.

For example, I want to write the decimal point ".", but the point is a metacharacter (encoded character with special meaning). If you want to represent the decimal point and not let it represent a special meaning, you need to escape: "\. " or [\.]

$lng = 125.97097735211630;<br>

##$pattern = "#^(0|[1-9][ 0-9]*)[\.][0-9]{14}$#";

//Numbers that satisfy 14 decimal places

preg_match ($pattern, $lng)

Backslash in expression<br>

In expression The backslash has multiple meanings, such as escaping, specifying a predefined character set, defining assertions, and displaying non-printable characters.

Escape characters<br>Escape characters mainly convert some special characters into ordinary characters. These commonly used special characters include ".", "?", "\", etc.

Specify the predefined character set

How to write regular expression in php

##Not displayed Printed characters

How to write regular expression in phpThe above content is for reference only!

Recommended video tutorial:

PHP video tutorial

The above is the detailed content of How to write regular expression in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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