


1.正则表达式基础知识
含义:由普通字符和(a-z)和一些特殊字符组成的字符串模式
功能:有效性验证。
替换文本。
从一个字符串提取一个子字符串。
分类:POSIX和Perl
POSIX风格更容易掌握,但不能用于二进制模式,而perl相对比较复杂。
2.POSIX风格的正则表达式
1.编写正则表达式
表4.3 POSIX正则表达式语法格式列表
字 符 |
描 述 |
\ |
转义字符,用于转义特殊字符。例如,'.'匹配单个字符,'\.'匹配一个点号。'\-'匹配连字符'-','\\'匹配符号'\' |
^ |
匹配输入字符串的开始位置。例如'^he'表示以'he'开头的字符串 |
$ |
匹配输入字符串的结束位置。例如,'ok$'表示以'ok'结尾的字符串 |
* |
匹配前面的子表达式零次或多次。例如,'zo*'能匹配"z"以及"zoo"。*等价于{0,} |
+ |
匹配前面的子表达式一次或多次。例如,'zo+'能匹配"zo"以及"zoo",但不能匹配"z"。+等价于{1,} |
? |
匹配前面的子表达式零次或一次。例如,'do(es)?'可以匹配"do"或"does"中的"do"。'?'等价于{0,1} |
{n} |
n是一个非负整数。匹配确定的n次。例如,'o{2}'不能匹配"Bob"中的'o',但是能匹配"food" 中的两个'o' |
{n,} |
n是一个非负整数。至少匹配n次。例如,'o{2,}'不能匹配"Bob"中的'o',但能匹配"foooood" 中的所有'o'。'o{1,}'等价于'o+'。'o{0,}'则等价于'o*' |
{n,m} |
m和n均为非负整数,其中n≤m。最少匹配n次且最多匹配m次。例如,"o{1,3}"将匹配"fooooood"中的前三个'o'。'o{0,1}'等价于'o?'。请注意在逗号和两个数之间不能有空格 |
? |
当该字符紧跟在任何一个其他限制符(*, +, ?, {n}, {n,}, {n,m})后面时,匹配模式是非贪婪的。非贪婪模式尽可能少地匹配所搜索的字符串,而默认的贪婪模式则尽可能多地匹配所搜索的字符串。例如,对于字符串"oooo",'o+?'将匹配单个"o",而'o+' 将匹配所有'o' |
. |
匹配除"\n"之外的任何单个字符,要匹配包括'\n' 在内的任何字符,可以使用'[.\n]'的模式 |
(pattern) |
匹配pattern并获取这一匹配。所获取的匹配保存到相应的数组中。要匹配圆括号字符,请使用 '\(' 或 '\)' |
(?:pattern) |
匹配pattern但不获取匹配结果,也就是说这是一个非获取匹配,不进行存储。这在使用"或"|"来组合一个模式的各个部分时很有用。例如,'industr(?:y|ies).就是一个比'industry|industries'更简略的表达式 |
(?=pattern) |
正向预查,在任何匹配pattern的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如,'Windows(?=95|98|NT|2000)'能匹配"Windows 2000"中的"Windows",但不能匹配"Windows 3.1"中的"Windows"。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始 |
(?!pattern) |
负向预查,在任何不匹配pattern的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如'Windows(?!95|98|NT|2000)'能匹配"'Windows 3.1"中的"Windows",但不能匹配"Windows 2000"中的"Windows"。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始 |
x|y |
匹配x或y。例如,'z|food' 能匹配"z"或"food",'(z|f)ood'则匹配"zood"或"food" |
[xyz] |
字符集合。匹配所包含的任意一个字符。例如,'[abc]'可以匹配"plain"中的'a' |
[^xyz] |
负值字符集合。匹配未包含的任意字符。例如,'[^abc]'可以匹配"plain"中的'p' |
[a-z] |
字符范围。匹配指定范围内的任意字符。例如,'[a-z]'可以匹配'a'到'z' 范围内的任意小写字母字符 |
[^a-z] |
负值字符范围。匹配不在指定范围内的任意字符。例如,'[^a-z]'可以匹配不在'a' 到'z'范围内的任意字符 |
●'[A-Za-z0-9] ': represents all uppercase letters, lowercase letters and numbers from 0 to 9.
●'^hello': Represents a string starting with hello.
●'world$': represents a string ending with world.
●'.at': Represents a string starting with any single character except "n" and ending with "at", such as "cat", "nat", etc.
●'^[a-zA-Z]': Represents a string starting with a letter.
●'hi{2}': Indicates the letter h followed by two i's, i.e. hii.
●'(go)+': Indicates a string containing at least one 'go' string, such as 'gogo'
ID card numbers generally consist of 18 digits or 17 digits followed by an X or Y letter To match the ID number, you can write:
^[0-9]{17}([0-9]|X|Y)$
The regular expression of the email address can be written:
^[a-zA-Z0-9-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$
2. String matching
ereg() and eregi() functions
Use the ereg() function to find when a string matches a substring, and return the length of the matching string. You can also return an array of matching characters with the help of parameters. The syntax format is as follows:
int ereg(string ($pattern) , string $string [, array $regs ])
/*This example checks whether the string is a date in ISO format (YYYY-MM-DD) */
$date="1988-08-09" ;
$len=ereg ('([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})', $date , $regs);//The date format is YYYY-MM-DD
if ($len)
{
echo "$regs[3].$regs[2].$regs[1]" . "
"; //Output "09.08.1988"
echo $regs[0] ."
"; //Output "1988-08-09"
echo $len; //Output 10
}
else
{
echo "Wrong date format: $date";
}
?>
3 .String replacement
ereg_replace() function syntax format is as follows:
string ereg_replace(string $pattern, string $replacement, string $string)
Description: The function uses string $replacement to replace string $string The part that matches $pattern and returns the replaced string. If no match is found,
$str ="hello world";
echo ereg_replace('[aeo]', 'x',$str). "
"; //Output 'hxllx wxrld'
$res='hello';
echo ereg_replace('hello', $res,$str); //Use hyperlink to replace 'hello'
?>
4.分割数组
使用split()函数可以完成与explode()函数一样的功能,而且可以根据给出的正则表达式来分割字符串,并返回一个数组。语法格式如下:
array split(string $pattern , string $string [, int $limit ])
5.产生正则表达式
3.Perl兼容的正则表达式
1.编写正则表达式
表4.4 Perl兼容正则表达式扩充的语法格式
字 符 |
描 述 |
\b |
匹配一个单词边界,也就是指单词和空格间的位置。例如,'er\b'可以匹配"never"中的 'er',但不能匹配"verb"中的'er' |
\B |
匹配非单词边界。'er\B'能匹配"verb"中的'er',但不能匹配"never"中的'er' |
\cx |
匹配由x指明的控制字符。例如,'\cM'匹配一个Control-M或回车符。x的值必须为A~Z或a~z之一。否则,将'c'视为一个原义的'c'字符 |
\d |
匹配一个数字字符。等价于'[0-9]' |
\D |
匹配一个非数字字符。等价于'[^0-9]' |
\f |
匹配一个换页符。等价于'\x0c'和'\cL' |
\n |
匹配一个换行符。等价于'\x0a'和'\cJ' |
\r |
匹配一个回车符。等价于'\x0d'和'\cM' |
\s |
匹配任何空白字符,包括空格、制表符、换页符等。等价于' [ \f\n\r\t\v] ' |
\S |
匹配任何非空白字符。等价于' [^ \f\n\r\t\v] ' |
\t |
匹配一个制表符。等价于'\x09'和'\cI' |
\v |
匹配一个垂直制表符。等价于'\x0b'和'\cK' |
\w |
匹配包括下划线的任何单词字符。等价于'[A-Za-z0-9_]' |
\W |
匹配任何非单词字符,等价于'[^A-Za-z0-9_]' |
\xn |
匹配n,其中n为十六进制转义值。十六进制转义值必须为确定的两个数字长。例如,'\x41' 匹配"A"。'\x041'则等价于'\x04' & "1"。正则表达式中可以使用ASCII编码 |
\num |
匹配num,其中num是一个正整数。对所获取的匹配的引用。例如,'(.)\1'匹配两个连续的相同字符 |
\n |
标志一个八进制转义值或一个后向引用。如果\n之前至少有n个获取得子表达式,则n为后向引用。否则,如果n为八进制数字(0~7),则n为一个八进制转义值 |
\nm |
标志一个八进制转义值或一个后向引用。如果\nm之前至少有nm个获取得子表达式,则 nm为后向引用。如果\nm之前至少有n个获取,则n为一个后跟文字m的后向引用。如果前面的条件都不满足,若 n和m均为八进制数字(0~7),则\nm将匹配八进制转义值nm |
\nml |
如果n为八进制数字(0~3),且m和l均为八进制数字(0~7),则匹配八进制转义值nml |
\un |
匹配n,其中n是用4个十六进制数字表示的Unicode字符。例如,'\u00A9'匹配版权符号(©) |
preg_match() function performs string search. The syntax format is as follows:
int preg_match(string $pattern, string $subject [, array $matches [, int $flags ]])
Description: The structure of this function is similar to the ereg() function, which searches the $subject string for content that matches the regular expression given by $pattern.
The preg_match() function returns the number of times $pattern is matched. Either 0 times (no match) or 1 time, because the preg_match() function will stop searching after the first match
There is also preg_match_all(), which continues to search from the end of the first match until the search is completed. the entire string.
The value of the preg_match_all() function parameter $flags can take the following three values:
●PREG_PATTERN_ORDER. The default item means that $matches[0] is an array of all pattern matches,
$matches[1] is an array of strings matched by the subpattern in the first bracket, and so on.
●PREG_SET_ORDER. If this flag is set, $matches[0] is the array of the first set of matches, $matches[1] is the array of the second set of matches, and so on.
●PREG_OFFSET_CAPTURE. PREG_OFFSET_CAPTURE can be used in combination with the other two tags.
If this tag is set, the associated string offset will also be returned for each matching result that occurs.
3. String replacement
Using the preg_replace() function can accomplish the same function as the function ereg_replace(), find matching substrings in the string, and replace the substring with the specified string.
The syntax format is as follows:
mixed preg_replace(mixed $pattern, mixed $replacement, mixed $subject [, int $limit])
4. String splitting
preg_split() function can use regular expressions The expression is used as a boundary to split a string, and the substring is stored in an array and returned. The function is similar to the split() function.
The syntax format is as follows:
array preg_split(string $pattern, string $subject [, int $limit [, int $flags ]])
Description: This function is case-sensitive and returns an array containing Substrings in $subject split along boundaries that match $pattern.
$limit is an optional parameter. If specified, up to $limit strings will be returned. If omitted or -1, there is no limit.
The value of $flags can be the following three:
●PREG_SPLIT_NO_EMPTY. If this flag is set, the function only returns a non-empty string.
●PREG_SPLIT_DELIM_CAPTURE. If this flag is set, matches of bracket expressions in delimiter patterns are also captured and returned.
PREG_SPLIT_OFFSET_CAPTURE. If this flag is set, the associated string offset will also be returned for each occurrence of a match.
4.3 Example - Verify form content
[Example 4.4] Use regular expressions to verify whether the form content entered by the user meets the format requirements.
Create a new EX4_4_Hpage.php file and enter the following code.
include 'EX4_4_Hpage.php'; //Include files EX4_4Hpage.php
$id=$_POST['ID'];
$pwd=$_POST['PWD'];
$phone=$_POST['PHONE'];
$Email =$_POST['EMAIL'];
$checkid=preg_match('/^w{1,10}$/',$id); //Check whether the string is within 10 characters
$checkpwd =preg_match('/^d{4,14}$/',$pwd); //Check whether it is between 4 and 14 numbers
$checkphone=preg_match('/^1d{10}$/' ,$phone); //Check whether it is an 11-digit number starting with 1
//Check the validity of the email address
$checkEmail=preg_match('/^[a-zA-Z0-9_-]+ @[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$/',$Email);
if($checkid&&$checkpwd&&$checkphone&&$checkEmail) //If If both are 1, the registration is successful
echo "Registration successful!";
else
echo "Registration failed, wrong format";
?>
新建EX4_4_Ppage.php文件,输入以下代码:
2. String matching
preg_match() function performs string search. The syntax format is as follows:
int preg_match(string $pattern, string $subject [, array $matches [, int $flags ]])
Description: The structure of this function is similar to the ereg() function, which searches the $subject string for content that matches the regular expression given by $pattern.
The preg_match() function returns the number of times $pattern is matched. Either 0 times (no match) or 1 time, because the preg_match() function will stop searching after the first match
There is also preg_match_all(), which continues to search from the end of the first match until the search is completed. the entire string.
The value of the preg_match_all() function parameter $flags can take the following three values:
●PREG_PATTERN_ORDER. The default item means that $matches[0] is an array of all pattern matches,
$matches[1] is an array of strings matched by the subpattern in the first bracket, and so on.
●PREG_SET_ORDER. If this flag is set, $matches[0] is the array of the first set of matches, $matches[1] is the array of the second set of matches, and so on.
●PREG_OFFSET_CAPTURE. PREG_OFFSET_CAPTURE can be used in combination with the other two tags.
If this tag is set, the associated string offset will also be returned for each matching result that occurs.
3. String replacement
Using the preg_replace() function can accomplish the same function as the function ereg_replace(), find matching substrings in the string, and replace the substring with the specified string.
The syntax format is as follows:
mixed preg_replace(mixed $pattern, mixed $replacement, mixed $subject [, int $limit])
4. String splitting
preg_split() function can use regular expressions The expression is used as a boundary to split a string, and the substring is stored in an array and returned. The function is similar to the split() function.
The syntax format is as follows:
array preg_split(string $pattern, string $subject [, int $limit [, int $flags ]])
Description: This function is case-sensitive and returns an array containing Substrings in $subject split along boundaries that match $pattern.
$limit is an optional parameter. If specified, up to $limit strings will be returned. If omitted or -1, there is no limit.
The value of $flags can be the following three:
●PREG_SPLIT_NO_EMPTY. If this flag is set, the function only returns a non-empty string.
●PREG_SPLIT_DELIM_CAPTURE. If this flag is set, matches of bracket expressions in delimiter patterns are also captured and returned.
PREG_SPLIT_OFFSET_CAPTURE. If this flag is set, the associated string offset will also be returned for each occurrence of a match.
4.3 Example - Verify form content
[Example 4.4] Use regular expressions to verify whether the form content entered by the user meets the format requirements.
Create a new EX4_4_Hpage.php file and enter the following code.
include 'EX4_4_Hpage.php'; //Include files EX4_4Hpage.php
$id=$_POST['ID'];
$pwd=$_POST['PWD'];
$phone=$_POST['PHONE'];
$Email =$_POST['EMAIL'];
$checkid=preg_match('/^w{1,10}$/',$id); //Check whether the string is within 10 characters
$checkpwd =preg_match('/^d{4,14}$/',$pwd); //Check whether it is between 4 and 14 numbers
$checkphone=preg_match('/^1d{10}$/' ,$phone); //Check whether it is an 11-digit number starting with 1
//Check the validity of the email address
$checkEmail=preg_match('/^[a-zA-Z0-9_-]+ @[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$/',$Email);
if($checkid&&$checkpwd&&$checkphone&&$checkEmail) //If If both are 1, the registration is successful
echo "Registration successful!";
else
echo "Registration failed, wrong format";
?>
新建EX4_4_Ppage.php文件,输入以下代码:
include 'EX4_4_Hpage.php'; //包含文件EX4_4Hpage.php
$id=$_POST['ID'];
$pwd=$_POST['PWD'];
$phone=$_POST['PHONE'];
$Email=$_POST['EMAIL'];
$checkid=preg_match('/^\w{1,10}$/',$id); //检查字符串是否在10个字符以内
$checkpwd=preg_match('/^\d{4,14}$/',$pwd); //检查是否在4-14个字符之间
$checkphone=preg_match('/^1\d{10}$/',$phone); //检查是否是以1开头的11位数子
//检查Email地址的合法性
$checkEmail=preg_match('/^[a-zA-Z0-9_\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/',$Email);
if($checkid&&$checkpwd&&$checkphone&&$checkEmail) //如果都为1,则注册成功
echo "注册成功!";
else
echo "注册失败,格式不对";
?>

随着数据的不断增长,数据分析和处理的需求也越来越重要。因此,现在越来越多的人开始将PHP和ApacheSpark集成来实现数据分析和处理。在本文中,我们将讨论什么是PHP和ApacheSpark,如何将二者集成到一起,并且用实例说明集成后的数据分析和处理过程。什么是PHP和ApacheSpark?PHP是一种通用的开源脚本语言,主要用于Web开发和服务

随着大数据时代的到来,数据处理变得越来越重要。对于各种不同的数据处理任务,不同的技术也应运而生。其中,Spark作为一种适用于大规模数据处理的技术,已经被广泛地应用于各个领域。此外,Go语言作为一种高效的编程语言,也在近年来得到了越来越多的关注。在本文中,我们将探讨如何在Go语言中使用Spark实现高效的数据处理。我们将首先介绍Spark的一些基本概念和原理

Vue3中的过滤器函数:优雅的处理数据Vue是一个流行的JavaScript框架,拥有庞大的社区和强大的插件系统。在Vue中,过滤器函数是一种非常实用的工具,允许我们在模板中对数据进行处理和格式化。Vue3中的过滤器函数有了一些改变,在这篇文章中,我们将深入探讨Vue3中的过滤器函数,学习如何使用它们优雅地处理数据。什么是过滤器函数?在Vue中,过滤器函数是

使用JavaSDK对接七牛云数据处理:如何实现数据转换和分析?概述:在云计算和大数据时代,数据处理是一个非常重要的环节。七牛云提供了强大的数据处理功能,可以对存储在七牛云中的各种类型的文件进行图像处理、音视频处理、文字处理等。本文将介绍如何使用JavaSDK对接七牛云的数据处理功能,并给出一些常用的代码示例。安装JavaSDK首先,我们需要在项目中引入

数据可视化是当前许多企业和个人在处理数据时非常关注的问题,它可以将复杂的数据信息转化为直观易懂的图表和图像,从而帮助用户更好地了解数据的内在规律和趋势。而PHP作为一种高效的脚本语言,在数据可视化方面也具有一定的优势,本文将介绍如何使用PHP进行数据可视化。一、了解PHP图表插件在PHP的数据可视化领域,大量的图表插件可以提供图表绘制、图表美化以及图表数据呈

PHP是一门广泛应用于Web开发的语言,通常被用来构建动态的Web应用程序。随着数据驱动型应用程序的兴起,PHP在数据分析和处理方面也变得越来越重要。本文将介绍如何使用PHP进行数据分析处理,从数据的获取、存储、分析和可视化展示等方面进行讲解。一、数据获取要进行数据分析处理,首先需要获取数据。数据可以来自各种不同的来源,例如数据库、文件、网络等。在PHP中,

随着互联网和信息技术的迅速发展,数据处理已经成为了现代计算机科学和工程学的一个重要研究领域,许多程序员和开发者都需要在他们的应用程序中处理大量数据。PHP作为一种简单易用的脚本语言,也逐渐成为了数据处理中的有力工具。在本文中,我们将介绍PHP中的一些批量数据处理技巧,以帮助开发者更高效地处理大量数据。使用for循环处理数据for循环是PHP中最基本的循环结构

在数据分析领域中,数据清洗是非常重要的环节。数据清洗包括识别和修改数据中的任何错误、表征与处理丢失或无效信息等。在Python中,有许多库可以帮助我们进行数据清洗。接下来,我们将介绍如何使用Python进行数据清洗。一、加载数据在Python中,可以使用pandas库来加载数据。当然,数据清洗之前需要对数据的类型进行检查。对于CSV文件,pandas中


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

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software