Home php教程 php手册 正则表达式的奥秘

正则表达式的奥秘

Jun 07, 2016 am 11:44 AM

正则表达式简介
转自:http://www.cnblogs.com/dtdd/archive/2013/03/10/2952988.html

1,正则表达式简介

(1)什么是正则表达式:是使用某种模式去匹配一类字符串的公式,它主要用于字符串的校验,分割,查找及替换操作。

(2)正则表达式的作用:替换文本或者代码中的某些字符,编辑软件无法完成的查找和替换,可以使用正则表达式来完成几乎全部的文本查找和替换工作。

正则表达式的强大功能源于它可以指定抽象模式来校验许多不同的字符序列。可以这定3种基本的抽象模式:

1.可以出现的字符集 (如:字母,数字和下划线)

2.可选择行的集合 (如:“com”,“net”或“org”)

3.可重复的序列 (如:自少一个短时不多于8个字母字符)

(将这3种模式按照不同的方式结合,就可以校验用户输入的电子右键地址格式是否正确;校验用户输入的日期格式是否正确;校验用户输入的身份证号格式是否正确等等 )

2,正则表达是基础

(1)元字符:正则表达式由一些普通字符和一些元字符(metacharacters)共同组成,其中普通字符包括大小写的字母和数字,而元字符则具有特殊含义。

元字符是用来描述其前面的字符在匹配模式中出现的方式。元字符本身是单一的字符,但是不同或者相同的元字符组合起来可以构成大的元字符。表中介绍各元字符的含义

字符 含义
() 将不同的祖父串组成一组,也称为子模式
[ ] 定义字符集合
{} 指定重复次数
^ 在字符串的开始匹配或者表示不在某个字符集内
$ 在字符传结尾匹配
. 匹配换行符(\n)之外的任意字符
? 重复零次或者一次
* 重复零次或者多次
+ 重复一次或者多次
\ 转换字符
| 或者选择分支的标识
正则表达式的语法是严格区分大小写的。

(2)方括号表达式:可以用来定义一个字符集合,其含义是必须匹配该集合中的任意一个字符,定义方括号表达式的方式有一下两种:

1.把所有的字符都列出阿来。 例如:方括号表达式”[1234567890]“可以与任何数字字符匹配,但是只能匹配一个字符。

2.利用字符”—“来表示字符区间。 例如:方括号表达式”[0123456789abcdefABCDEF]“ 可以简写成方括号表达式”[0-9a-fA-F]“。

(通常情况下,字符”-“都只是一个普通字符,只有在表达范围时才被作为元字符来使用。如:方括号表达式[-a-z]的第一个字符”-“只代表一个普通字符,而第二个是一个元字符。

用来表示字母a到z的范围里)

常用的方括号表达式:

字符 含义
[a-z] 小写字母字符
[A-Z] 大写字母字符
[a-zA-Z] 大小写字母字符
[0-9] 数字字符
[0-9\+\-\*\\] 数字字符、加号、减号、乘号、除号
[\f\r\n\t] 空白字符
(3)定位符:方括号表达式只能用来匹配一个字符,如“Y8”、“B3”、“A6”等。这里就需要使用定位符“^”与“$”

定位符是用来描述字符串或单词的边界的,具体包括^("shift+6组合键")和$(美元符号)它们含义分别为在字符传的开始匹配和在结尾匹配。

灵活使用定位符和方括号可以完成功能不同的正则表达式。 如:“^[A-Z][0-9]$”可以用来校验由两个字符组成的字符串,并且该字符串要同时满足

以大小写字母字符开头,以数字结尾的条件。

元字符“^”如果出现在方括号表达式的里面,表示含义为否;如果出现在方括号外面,表示含义为字符串的开始。

(4)量词:也称为限定符,用来指定字符必须要出现的次数,也就是说在指定重复模式时要使用量词。 量词具体包括*、+、?、{n}、{n,}、{n,m}共6种使用见表所示:

字符 含义
* 重复任意次(包括零次)
+ 重复一次或多次
? 重复零次或一次
{n} 重复n次,其中n是个非负数
{n,} 重复至少n次,其中n是非负数
{n,m} 重复至少n到m次,其中m和n均为非负数并且m大于等n
使用量词可以指定在一次搜索中查找到一个表达式的次数,如:正则表达式“sung{1,2}”可以用来校验任何字符串“sung”和“sungsung”

量词“?”如果出现在方括号里面也一样代表一个普通字符。如:“[-0-9?]”可以用来校验任何一个数字或者一个普通字符"?"或者一个普通字符“-”。

(5)选择符:正则表达式中竖线“|”可以表示选择分支。如:正则表达式“aaa|bbb|ccc”可以用来校验字符串“aaa”,字符串“bbb”,或“ccc”。 使用选择与其他元字符结合可以完成更复杂的正则表达式 如:正则表达式“^[0-9]|[a-z]”可以用来校验数字或字母字符开头。

AD:真正免费,域名+虚机+企业邮箱=0元

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)

Learn about introductory code examples for Python programming Learn about introductory code examples for Python programming Jan 04, 2024 am 10:50 AM

Learn about Python programming with introductory code examples Python is an easy-to-learn, yet powerful programming language. For beginners, it is very important to understand the introductory code examples of Python programming. This article will provide you with some concrete code examples to help you get started quickly. Print HelloWorldprint("HelloWorld") This is the simplest code example in Python. The print() function is used to output the specified content

Go language programming examples: code examples in web development Go language programming examples: code examples in web development Mar 04, 2024 pm 04:54 PM

"Go Language Programming Examples: Code Examples in Web Development" With the rapid development of the Internet, Web development has become an indispensable part of various industries. As a programming language with powerful functions and superior performance, Go language is increasingly favored by developers in web development. This article will introduce how to use Go language for Web development through specific code examples, so that readers can better understand and use Go language to build their own Web applications. 1. Simple HTTP Server First, let’s start with a

PHP variables in action: 10 real-life examples of use PHP variables in action: 10 real-life examples of use Feb 19, 2024 pm 03:00 PM

PHP variables store values ​​during program runtime and are crucial for building dynamic and interactive WEB applications. This article takes an in-depth look at PHP variables and shows them in action with 10 real-life examples. 1. Store user input $username=$_POST["username"];$passWord=$_POST["password"]; This example extracts the username and password from the form submission and stores them in variables for further processing. 2. Set the configuration value $database_host="localhost";$database_username="username";$database_pa

Java implements simple bubble sort code Java implements simple bubble sort code Jan 30, 2024 am 09:34 AM

The simplest code example of Java bubble sort Bubble sort is a common sorting algorithm. Its basic idea is to gradually adjust the sequence to be sorted into an ordered sequence through the comparison and exchange of adjacent elements. Here is a simple Java code example that demonstrates how to implement bubble sort: publicclassBubbleSort{publicstaticvoidbubbleSort(int[]arr){int

From beginner to proficient: Code implementation of commonly used data structures in Go language From beginner to proficient: Code implementation of commonly used data structures in Go language Mar 04, 2024 pm 03:09 PM

Title: From Beginner to Mastery: Code Implementation of Commonly Used Data Structures in Go Language Data structures play a vital role in programming and are the basis of programming. In the Go language, there are many commonly used data structures, and mastering the implementation of these data structures is crucial to becoming a good programmer. This article will introduce the commonly used data structures in the Go language and give corresponding code examples to help readers from getting started to becoming proficient in these data structures. 1. Array Array is a basic data structure, a group of the same type

Huawei Cloud Edge Computing Interconnection Guide: Java code examples to quickly implement interfaces Huawei Cloud Edge Computing Interconnection Guide: Java code examples to quickly implement interfaces Jul 05, 2023 pm 09:57 PM

Huawei Cloud Edge Computing Interconnection Guide: Java Code Samples to Quickly Implement Interfaces With the rapid development of IoT technology and the rise of edge computing, more and more enterprises are beginning to pay attention to the application of edge computing. Huawei Cloud provides edge computing services, providing enterprises with highly reliable computing resources and a convenient development environment, making edge computing applications easier to implement. This article will introduce how to quickly implement the Huawei Cloud edge computing interface through Java code. First, we need to prepare the development environment. Make sure you have the Java Development Kit installed (

How to use PHP to write inventory management function code in the inventory management system How to use PHP to write inventory management function code in the inventory management system Aug 06, 2023 pm 04:49 PM

How to use PHP to write the inventory management function code in the inventory management system. Inventory management is an indispensable part of many enterprises. For companies with multiple warehouses, the inventory management function is particularly important. By properly managing and tracking inventory, companies can allocate inventory between different warehouses, optimize operating costs, and improve collaboration efficiency. This article will introduce how to use PHP to write code for inventory warehouse management functions, and provide you with relevant code examples. 1. Establish the database before starting to write the code for the inventory warehouse management function.

Guidance and Examples: Learn to implement the selection sort algorithm in Java Guidance and Examples: Learn to implement the selection sort algorithm in Java Feb 18, 2024 am 10:52 AM

Java Selection Sorting Method Code Writing Guide and Examples Selection sorting is a simple and intuitive sorting algorithm. The idea is to select the smallest (or largest) element from the unsorted elements each time and exchange it until all elements are sorted. This article will provide a code writing guide for selection sorting, and attach specific Java sample code. Algorithm Principle The basic principle of selection sort is to divide the array to be sorted into two parts, sorted and unsorted. Each time, the smallest (or largest) element is selected from the unsorted part and placed at the end of the sorted part. Repeat the above

See all articles