Home > Backend Development > PHP Problem > What does regular expression mean in php

What does regular expression mean in php

青灯夜游
Release: 2023-03-12 21:18:01
Original
1999 people have browsed it

In PHP, a regular expression is a logical formula for string operations. It uses some predefined specific characters and a combination of these specific characters to form a "rule string". This "Rule string" can be used to express a filtering logic for strings. Regular expressions are an important and complex technique when working with text data.

What does regular expression mean in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

What is a regular expression

Regular expression is a logical formula for string operations. It uses some predefined specific characters and combinations of these specific characters to form a "regular string". This "regular character" "String" is used to express a filtering logic for strings. For example, when registering a user, the user is often required to fill in data in a specific format such as email, phone number, and birthday. In order to prevent the user from randomly filling in some invalid data, regular expressions are used at this time.

Regular expressions are also called pattern expressions. They have a very complete syntax system for writing patterns, and provide a flexible and intuitive string processing method. Regular expressions construct patterns with specific rules, compare them with input string information, and use them in specific functions to achieve operations such as string matching, search, replacement, and segmentation.

To give an example in our daily life, if you want to search for all txt format files in a certain directory on your computer, you can enter *.txt in the directory and then press the Enter key. List all txt format files in the directory. The *.txt used here can be understood as a simple regular expression.

The following two examples are constructed using the syntax of regular expressions, as shown below:

/http(s)?:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is      // 匹配网址 URL 的正则表达式
/^\w{3,}@([a-z]{2,7}|[0-9]{3})\.(com|cn)$/                    // 匹配邮箱地址的正则表达式
Copy after login

Don’t be deterred by the seemingly garbled strings in the above examples, they are expressed according to regular expressions It is a string composed of ordinary characters and characters with special functions. And these strings must be used in specific regular expression functions to be effective.

The main purpose of using regular expressions is to achieve powerful functions in a simple way. In order to be simple, effective and powerful, regular expression rules are complicated, and it is even more difficult to construct correct and effective regular expressions, so it takes some effort to learn regular expressions well.

The purpose of regular expressions

Regular expression describes a string matching pattern, which can be used to check whether a string contains a certain substring, Replace the matching substring or extract the substring that meets a certain condition from a certain string, etc. For example, when a user submits a form, to determine whether the entered phone number, email address, etc. is valid, ordinary literal-based character verification is obviously not enough.

Regular expressions are literal patterns composed of ordinary characters (such as the characters a through z) and special characters (called "metacharacters"). A regular expression acts as a template that matches a character pattern with a searched string. A regular expression pattern can be a single character, a collection of characters, a range of characters, a selection between characters, or any combination of all these components.

The purpose of using regular expressions is to achieve powerful functions in a simple way. In order to be simple, effective and powerful, the regular expression rules are complicated. It is even more difficult to construct correct and effective regular expressions, so it requires some effort. After getting started, through certain reference and a lot of practice, it is quite effective and interesting to use regular expressions in development practice.

Using regular expressions in PHP

PHP has two sets of regular expression processing operations supported by the function library:

One set is provided by PCRE (Perl Compatible Regular Expression) library provides regular expression functions compatible with the Perl language, with preg_ as the prefix name of the function;

The other set is POSIX (Portable Operating System Interface) Extended syntax regular expression function, with ereg_ as the prefix of the function.

The functions of the two function libraries are similar, but the execution efficiency of PCRE is higher than that of POSIX, so we only introduce the PCRE function library.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What does regular expression mean in php. 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