The regular expression of PHP

Source: Internet
Author: User

Regular expressions are a highly versatile technique in which almost all (or most) programming languages support regular expressions.

Introduction of regular expressions

What is a regular expression?

When writing a program or Web page that handles strings, there is often a need to find strings that match some of the complex rules. Regular expressions are the syntax used to describe these rules.

In determining the format of a user's e-mail address and the format of a mobile phone number

The main role is: segmentation, matching, find, replace

Regular expressions are a crucial point of knowledge for a programmer, so it's important for every programmer to learn the right things. Not only can we do some work that we can't do through the function, but it also helps us to reduce a lot of effort.

Perl-style Regular expression syntax

Pattern rule:/php/with two diagonal lines before and after the string

Matching function: the Preg_match () function searches for a pattern in a string, or returns a fake if there is a return true

Cases:

<?php
Header ("Content-type:text/html;charset=utf-8");
$mode = "/php/"; Rule pattern
$string = "I am studying PHP";
if (Preg_match ($mode, $string)) {
echo "matches successfully! ";
}else{
echo "does not match!" ";
}
?>

Elements in regular expressions

The regular expression contains three elements: Quantifiers, metacharacters, and modifiers

Order of operations in regular expressions

Follow the rules of operation from left to right, as shown in the following figure:

Url:http://www.bianceng.cn/webkf/php/201701/50525.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.