Home Backend Development PHP Tutorial PHP下常用正则表达式整理_PHP

PHP下常用正则表达式整理_PHP

Jun 01, 2016 pm 12:17 PM
Commonly used regular expression

正则表达式

--------------------------------------------------------- 正则收藏

手机号码:
$mode = "/^1[358]\d{9}/";

邮箱地址:
$mode = "/^[a-z][-_\.]?[a-z\d]*@[a-z0-9]+[\.][a-z]{2,4}/i";

---------------------------------------------------------- 正则基础

$mode = "/^1[358]\d{9}/i";
匹配模块必须以 / / 开始和结束,第二个 / 后可以加模式修正符

原子
①a-z A-Z _ 0-9 //最常见的字符
②(abc) //用圆括号括起来起来的单元符号
③[abcs] [^abd] //用方括号括起来的原子表,
原子表中的^代表排除或相反内容

\d 包含所有数字[0-9]
\D 除所有数字外[^0-9]
\w 包含所有英文字符[a-zA-Z_0-9]
\W 除所有英文字符外[^a-zA-Z_0-9]
\s 包含空白区域如回车、换行、分页等 [\f\n\r]

元字符
* 匹配前一个内容的0次1次或多次
+ 1次或多次
? 0次或1次
. 代表任意一个字符(除了回车换行)
| 相当与php的 || (“或”的意思)
^ 强制匹配字符串首部内容
$ 强制匹配字符串尾部内容
[^abc] 匹配除了a或b或c之外的内容
\b 匹配单词边界,边界可以是空格或者特殊符号
\B 匹配除带单词边界以外的内容
{m} 匹配前一个内容的重复次数为M次
{m,} 匹配前一个内容的重复次数大于等于M次
{m,n} 匹配前一个内容的重复次数M次到N次
( ) 整体匹配,并放入内存,可使用\\1 或 \\2 …依次获取

优先级:依次降低
( ) 圆括号因为是内存处理所以最高
* ? + { } 重复匹配内容其次
^ $ \b 边界处理第三
| 条件处理第四
最后按照运算顺序计算匹配

常用修正符: $mode = "/正则/U";
i 正则内容在匹配时候不区分大小写(默认是区分的)
m 在匹配首内容或者尾内容时候采用多行识别匹配
S 将回车转化为空格
x 忽略正则中的空白
A 强制从头开始匹配
D 强制$匹配尾部无任何内容 \n
U 禁止贪婪匹配,只跟踪到最近的一个匹配符并结束,
常用在采集程序上的正则表达式

应用
preg_match_all ( string pattern, string subject, array matches [, int flags] )
截取比较详细的内容,采集网页,分析文本
preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit] )
preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit] )
提示 1、替换内容可以是一个正则也可以是数组正则
2、替换内容可以通过修正符e来解决替换执行内容
preg_split ( string pattern, string subject [, int limit [, int flags]] )
通过正则表达式来切割相关内容,类似之前学过的explode切割函数,但explode
只能按照一种方式切割有局限性。
------------------------------------------------- 调试代码
[code]
$mode = "/^[a-z][-_\.]?[a-z\d]*@[a-z0-9]+[\.][a-z]{2,4}/i";
$str = "a12345@bitsCN.com";
echo $str.'
';
if(preg_match($mode, $str, $arr)){
echo 'succeed -- '.$arr[0];
}else{
echo 'failed';
}
?>
[code]
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP regular expression validation: number format detection PHP regular expression validation: number format detection Mar 21, 2024 am 09:45 AM

PHP regular expression verification: Number format detection When writing PHP programs, it is often necessary to verify the data entered by the user. One of the common verifications is to check whether the data conforms to the specified number format. In PHP, you can use regular expressions to achieve this kind of validation. This article will introduce how to use PHP regular expressions to verify number formats and provide specific code examples. First, let’s look at common number format validation requirements: Integers: only contain numbers 0-9, can start with a plus or minus sign, and do not contain decimal points. floating point

How to validate email address in Golang using regular expression? How to validate email address in Golang using regular expression? May 31, 2024 pm 01:04 PM

To validate email addresses in Golang using regular expressions, follow these steps: Use regexp.MustCompile to create a regular expression pattern that matches valid email address formats. Use the MatchString function to check whether a string matches a pattern. This pattern covers most valid email address formats, including: Local usernames can contain letters, numbers, and special characters: !.#$%&'*+/=?^_{|}~-`Domain names must contain at least One letter, followed by letters, numbers, or hyphens. The top-level domain (TLD) cannot be longer than 63 characters.

How to match timestamps using regular expressions in Go? How to match timestamps using regular expressions in Go? Jun 02, 2024 am 09:00 AM

In Go, you can use regular expressions to match timestamps: compile a regular expression string, such as the one used to match ISO8601 timestamps: ^\d{4}-\d{2}-\d{2}T \d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-][0-9]{2}:[0-9]{2})$ . Use the regexp.MatchString function to check if a string matches a regular expression.

Master regular expressions and string processing in Go language Master regular expressions and string processing in Go language Nov 30, 2023 am 09:54 AM

As a modern programming language, Go language provides powerful regular expressions and string processing functions, allowing developers to process string data more efficiently. It is very important for developers to master regular expressions and string processing in Go language. This article will introduce in detail the basic concepts and usage of regular expressions in Go language, and how to use Go language to process strings. 1. Regular expressions Regular expressions are a tool used to describe string patterns. They can easily implement operations such as string matching, search, and replacement.

Learn the canvas framework and explain the commonly used canvas framework in detail Learn the canvas framework and explain the commonly used canvas framework in detail Jan 17, 2024 am 11:03 AM

Explore the Canvas framework: To understand what are the commonly used Canvas frameworks, specific code examples are required. Introduction: Canvas is a drawing API provided in HTML5, through which we can achieve rich graphics and animation effects. In order to improve the efficiency and convenience of drawing, many developers have developed different Canvas frameworks. This article will introduce some commonly used Canvas frameworks and provide specific code examples to help readers gain a deeper understanding of how to use these frameworks. 1. EaselJS framework Ea

Spring Annotation Revealed: Analysis of Common Annotations Spring Annotation Revealed: Analysis of Common Annotations Dec 30, 2023 am 11:28 AM

Spring is an open source framework that provides many annotations to simplify and enhance Java development. This article will explain commonly used Spring annotations in detail and provide specific code examples. @Autowired: Autowired @Autowired annotation can be used to automatically wire beans in the Spring container. When we use the @Autowired annotation where dependencies are required, Spring will find matching beans in the container and automatically inject them. The sample code is as follows: @Auto

PHP regular expressions: exact matching and exclusion of fuzzy inclusions PHP regular expressions: exact matching and exclusion of fuzzy inclusions Feb 28, 2024 pm 01:03 PM

PHP Regular Expressions: Exact Matching and Exclusion Fuzzy inclusion regular expressions are a powerful text matching tool that can help programmers perform efficient search, replacement and filtering when processing text. In PHP, regular expressions are also widely used in string processing and data matching. This article will focus on how to perform exact matching and exclude fuzzy inclusion operations in PHP, and will illustrate it with specific code examples. Exact match Exact match means matching only strings that meet the exact condition, not any variations or extra words.

How to verify password using regular expression in Go? How to verify password using regular expression in Go? Jun 02, 2024 pm 07:31 PM

The method of using regular expressions to verify passwords in Go is as follows: Define a regular expression pattern that meets the minimum password requirements: at least 8 characters, including lowercase letters, uppercase letters, numbers, and special characters. Compile regular expression patterns using the MustCompile function from the regexp package. Use the MatchString method to test whether the input string matches a regular expression pattern.

See all articles