正则表达式简介_PHP
正则表达式
有些新手对正则表达式不是很熟悉,有必要在此作一简单回顾。如果你是正则表达式高手,可以不用看这一部分。
正则表达式是描述字符串集的字符串。例如,正则表达式“Mic*”描述所有包含“Mic”,后跟零个或多个字符的字符串。Mickey、Microsoft、Michelangelo 或 Mic 本身都是例子。句号“.”匹配任何字符,“+”类似“*”,但至少要一个字符,所以“Mic+”匹配前述所有除“Mic”以外的串。[a-z]指一个匹配范围,所以[a-zA-Z_0-9]匹配字母、数字或下划线。Regex 称之为单词字符,可以将它写成“\w”。所以“\w+”匹配至少有一个字符的单词字符序列——换句话说,叫 C 符号(C tokens)。那么这样一来,几乎所有的C 符号都不能以数字开头,因此,下面这个正则表达式是正确的:“^[a-zA-Z_]\w*$”。专用字符“^”意思是“以...开始”(除非它位于某个范围之内,这时它的意思是“非”),“$”意思是“结尾”,那么“^[a-zA-Z_]\w*$”意思就是:以字母或下划线开始的字母、数字或下划线字符串。
正则表达式在对输入进行有效性验证时非常有用。\d 匹配数字,{n}匹配重复n次,于是 ^5\d{15}$ 匹配5开头的16位数字,也即是说 MasterCard 信用卡号码。那 ^[45]\d{15}$ 就是Visa 卡号,它以4开头。你可以用大括弧对表达式进行分组,下面是个测试。这个表达式描述的是什么呢?
^\d{5}(-\d{4}){0,1}$
提示:{0,1} 意思是重复0次或1次(可以缩写成问号 ?)。想出来了吗?该表达式意思是:五个数字后重复0次或1次(破折号后跟四个数字)。它匹配 02142和98007-4235,但不匹配 3245 或 2345-98761。这也就是美国的邮政编码。大括弧将 ZIP+4 部分分组,所以{0,1}修饰符将应用于整个分组。
以上我仅浅尝即止地说明了正则表达式能做什么。我还没提到替换,由于我没有具体资料,所以不敢描述在 Unicode 中会怎么样。但你能感觉到正则表达式有多么强大。多年来它们乃 UNIX 的中流砥柱,并且在Web 编程和 Perl 这样的语言中更臻完善,其对 HTML 的操作几乎完全是对文本的处理。正则表达式在 Windows 中一直没有得到充分使用,直到 .NET 框架面世,它才正式成为 Windows 家族的一员。
框架 Regex 类
.NET 框架用 Regex 类实现正则表达式,并有三个支持类:Match、Group 和 Capture (参见 Figure A)。典型情况下,你创建 Regex 并用输入串调用 Regex::Match 来获得第一个 Match,或用 Regex::Matches 来获取所有匹配:
Regex *r = new Regex("\b\w+\b");
MatchCollection* mc =
r->Matches("abc ,_foo ,for (int i=0; i
Match *m = mc->Index(i);
Console.WriteLine(m->Value);
}
这将显示“abc”,“foo”和“mumble7”,每个匹配在一行。这个例子引入了一个专门的字符 \b,所谓锚或原子零宽度断言,就像 ^(开始)和$(结尾)。\b 指定某个单词的边界,所以“\b\w+\b”意思是用单词分隔的一个或多个单词字符。
Figure A Regex 类
正则表达式中的每个括弧表达式都构成一个 Group。Regex::Groups 返回作为集合的 Groups,它决不会是空,因为整个正则表达式本身即是一组。Groups 很重要,因为它们使你进行逻辑 OR 匹配,如“(ying|yong)”,它们使你将限定符应用到子表达式,并让你吸取匹配的单独部分。正文的 Figure 1 中我的 RegexTest 程序运行后用邮编为例显示分组。
在所有函数中最强大的函数要数 Regex::Replace,它使得正则表达式的威力惊人地强大。和许多开发人员一样,过去在多次传递字符串到多行编辑控件之前,我常常不得不手工将 “\n” 转换为“\r\n”,但使用 Regex::Replace,这个操作简直易如反掌。
s = Regex::Replace(s,"\n","\r\n");
Regex::Match 和 Replace 具备静态重载,所以你可以不用创建对象,以快速使用正则表达式。我最喜欢的 Regex::Replace 重载之一是带有一个委托参数,使你能用过程代码动态计算替换文本——参见正文中那个有趣的例子。
一些警告:每一种正则表达式的实现是有不太一样的。例如,在 Perl 中,{,1}是{0,1}的速记版,而微软的老大们不是那样做的。要当心一些微小的差别。

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

AI Hentai Generator
Generate AI Hentai for free.

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



Correctly displaying Chinese characters in matplotlib is a problem often encountered by many Chinese users. By default, matplotlib uses English fonts and cannot display Chinese characters correctly. To solve this problem, we need to set the correct Chinese font and apply it to matplotlib. Below are some specific code examples to help you display Chinese characters correctly in matplotlib. First, we need to import the required libraries: importmatplot

Object-relational mapping (ORM) frameworks play a vital role in python development, they simplify data access and management by building a bridge between object and relational databases. In order to evaluate the performance of different ORM frameworks, this article will benchmark against the following popular frameworks: sqlAlchemyPeeweeDjangoORMPonyORMTortoiseORM Test Method The benchmarking uses a SQLite database containing 1 million records. The test performed the following operations on the database: Insert: Insert 10,000 new records into the table Read: Read all records in the table Update: Update a single field for all records in the table Delete: Delete all records in the table Each operation

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.

Object-relational mapping (ORM) is a programming technology that allows developers to use object programming languages to manipulate databases without writing SQL queries directly. ORM tools in python (such as SQLAlchemy, Peewee, and DjangoORM) simplify database interaction for big data projects. Advantages Code Simplicity: ORM eliminates the need to write lengthy SQL queries, which improves code simplicity and readability. Data abstraction: ORM provides an abstraction layer that isolates application code from database implementation details, improving flexibility. Performance optimization: ORMs often use caching and batch operations to optimize database queries, thereby improving performance. Portability: ORM allows developers to

PHP String Matching Tips: Avoid Ambiguous Included Expressions In PHP development, string matching is a common task, usually used to find specific text content or to verify the format of input. However, sometimes we need to avoid using ambiguous inclusion expressions to ensure match accuracy. This article will introduce some techniques to avoid ambiguous inclusion expressions when doing string matching in PHP, and provide specific code examples. Use preg_match() function for exact matching In PHP, you can use preg_mat

Understanding Java Design Patterns: An introduction to 7 commonly used design patterns, specific code examples are required. Java design patterns are a universal solution to software design problems. It provides a set of widely accepted design ideas and codes of conduct. Design patterns help us better organize and plan the code structure, making the code more maintainable, readable and scalable. In this article, we will introduce 7 commonly used design patterns in Java and provide corresponding code examples. Singleton Patte

How to use Golang to determine whether a character is a letter. In Golang, determining whether a character is a letter can be achieved by using the IsLetter function in the Unicode package. The IsLetter function checks whether the given character is a letter. Next, we will introduce in detail how to use Golang to write code to determine whether a character is a letter. First, you need to create a new Go file in which to write the code. You can name the file "main.go". code

Jedi Submarine 2 is a third-person shooting game with high-quality masterpiece gameplay. It has a lot of exciting gameplay that allows friends to explore the operational fun of online shooting battles. The online mode in the game can be matched. Some players I still don’t know how to operate matching. In this issue, I will share the matching steps with you! Matching operation tutorial of Jedi Submarine 2. Answer: Click Quick Match on the planet interface. The matching method of Jedi Submarine 2. The quick matching of Jedi Submarine 2 is a very good function. It can help players find teammates to match together, enter a mission together, and cooperate with each other to obtain a higher mission evaluation. The matching options are on the planet interface. When looking for tasks or viewing public rooms, there will be a quick match below. Click to start matching. If the player turns on cross leveling
