PHP学习正则表达式 课件第1/2页
正则表达式
在PHP中有两套正则表达式函数库,两者功能相似,只是执行效率略有差异:
一套是由PCRE(Perl Compatible Regular Expression)库提供的。使用“preg_”为前缀命名的函数;
一套由POSIX(Portable Operating System Interface of Unix )扩展提供的(PHP默认)。使用以“ereg_”为前缀命名的函数;
PHP中,正则表达式有三个作用:
匹配,也常常用于从字符串中析取信息。
用新文本代替匹配文本。
将一个字符串拆分为一组更小的信息块。
一个正则表达式中至少包含一个原子。
原子(普通字符,如英文字符)
元字符(有特殊功用的字符)
模式修正字符(对正则表达式语义的修正)
原子(Atom)
单个字符、数字,如a~z,A~Z,0~9。
模式单元,如(ABC)可以理解为由多个原子组成的大的原子。
原子表,如 [ABC]。
重新使用的模式单元,如:\\1
普通转义字符,如:\d, \D, \w
转义元字符,如:\*,\.
POSIX正则表达式
POSIX正则表达式全称为Portable Operating System Interface of Unix,意为UNIX可移植操作系实现接口。
构造POSIX正则表达式的方法和创建数学表达式的方法一样,也就是用多种元字符与操作符将小的表达式结合在一起来创建更大的表达式。
元字符(Meta-character)
元字符是用于构造规则表达式的具有特殊含义的字符。如果要在正则表达式中包含元字符本身,必须在其前加上”\”进行转义
元字符 说明
* 0次、1次或多次匹配其前的原子
+ 1次或多次匹配其前的原子
? 0次或1次匹配其前的原子
| 匹配两个或多个选择 列如 [1-9]|[a-b]|[A-Z] 与其中任何匹配为ture
^ 匹配字符串串首的原子 例如 abscd===^afdgfgf 相匹配
$ 匹配字符串串尾的原子 例如 dasdsv===v$
[] 匹配方括号中的任一原子 例如 s===[dsadas]
[^] 匹配除方括号中的原子外的任何字符 例如 aaaaa===[dddd]
{m} 表示其前原子恰好出现m次
{m,n} 表示其前原子至少出现m次,至少出现n次(n>m)
{m,} 表示其前原子出现不少于m次
() 整体表示一个原子
. 匹配除换行之外的任何一个字符
^ $ 这两个原字符在一起称为定界
abd===^abc$ 只有这样才匹配
模式匹配的顺序
顺序 元字符 说明
1 () 模式单元
2 ?* +{} 重复匹配
3 ^$ 边界限制
4 | 模式选择
POSIX正则表达式函数
ereg()和eregi()
ereg_replace()和eregi_replace()
split()和spliti()
ereg()和eregi()ereg()字符串匹配函数,eregi()是ereg()函数的忽略大小的版本
语法格式:if (!ereg('^[^./][^/]*$', $userfile))//不匹配格式输出die
{
die('这是一个非法的文件名!');
}
ereg_replace()和eregi_replace(忽略大小写)替换
string eregi_replace (“正则表达式”,“目标替换字符”,“替换目标”)
语法格式:$string = "This is a test";
echo str_replace(" is", " was", $string);
echo ereg_replace("( )is", "\\1was", $string);\\1 为继承第一个整体
echo ereg_replace("(( )is)", "\\2was", $string);\\2继承第二个整体
split()和spliti(忽略大小写)用正则表达式将字符串分割到数组中
list:给数组中的值赋予一些变量
语法格式:$date = "04/30/1973";
list($month, $day, $year) = split ('[/.-]', $date);//列出三个变数对应格式//以什么形式拆分 拆分谁
echo "Month: $month; Day: $day; Year: $year
\n";
输出结果Month: 04; Day: 30; Year: 1973

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
