Home Backend Development PHP Tutorial 求问各种正则表达式后缀的意义解决方案

求问各种正则表达式后缀的意义解决方案

Jun 13, 2016 pm 01:52 PM
linux php quot

求问各种正则表达式后缀的意义
关于php中正则表达式后缀的意义,现在的我是一知半解,只是知道/u表非贪婪匹配,/i表示忽略大小写,/s可以让 ". "匹配到换行符。但不知道其他后缀有什么意义,所以想系统的问一下还望这方面的前辈指教一下。

------解决方案--------------------
在PHP中使用与Perl兼容的正则表达式
--------------------------------------------

来源:PHP动力在线
1 前言
PHP被大量的应用于Web的后台CGI开发,通常是在用户数据数据之后得出某种结果,但是如果用户输入的数据不正确,就会出现问题,比如说某人的生日是 "2月30日 "!那应该怎么样来检验暑假是否正确呢? 在PHP中加入了正则表达式的支持,让我们可以十分方便的进行数据匹配。

2 什么是正则表达式:
简单的说,正则表达式是一种可以用于模式匹配和替换的强大工具。在几乎所有的基于UNIX/LINUX系统的软件工具中找到正则表达式的痕迹,例如:Perl或PHP脚本语言。此外,JavaScript这种客户端的脚本语言也提供了对正则表达式的支持,现在正则表达式已经成为了一个通用的概念和工具,被各类技术人员所广泛使用。
在某个Linux网站上面有这样的话: "如果你问一下Linux爱好者最喜欢什么,他可能会回答正则表达式;如果你问他最害怕什么,除了繁琐的安装配置外他肯定会说正则表达式。 "
正如上面说的,正则表达式看起来非常复杂,让人害怕,大多数的PHP初学者都会跳过这里,继续下面的学习,但是PHP中的正则表达式有着可以利用模式匹配找到符合条件的字符串、判断字符串是否合乎条件或者用指定的字符串来替代符合条件的字符串等强大的功能,不学实在太可惜了……


3 正则表达式的基本语法:
一个正则表达式,分为三个部分:分隔符,表达式和修饰符。
分隔符可以是除了特殊字符以外的任何字符(比如 "/ ! "等等),常用的分隔符是 "/ "。表达式由一些特殊字符(特殊字符详见下面)和非特殊的字符串组成,比如 "[a-z0-9_-]+@[a-z0-9_-.]+ "可以匹配一个简单的电子邮件字符串。修饰符是用来开启或者关闭某种功能/模式。下面就是一个完整的正则表达式的例子:
/hello.+?hello/is
上面的正则表达式 "/ "就是分隔符,两个 "/ "之间的就是表达式,第二个 "/ "后面的字符串 "is "就是修饰符。
在表达式中如果含有分隔符,那么就需要使用转义符号 "\ ",比如 "/hello.+?\/hello/is "。转义符号除了用于分隔符外还可以执行特殊字符,全部由字母构成的特殊字符都需要 "\ "来转义,比如 "\d "代表全体数字。


4 正则表达式的特殊字符:
正则表达式中的特殊字符分为元字符、定位字符等等。
元字符是正则表达式中一类有特殊意义的字符,用来描述其前导字符(即元字符前面的字符)在被匹配的对象中出现的方式。元字符本身是一个个单一的字符,但是不同或者相同的元字符组合起来可以构成大的元字符。
元字符:
大括号:大括号用来精确指定匹配元字符出现的次数,例如 "/pre{1,5}/ "表示匹配的对象可以是 "pre "、 "pree "、 "preeeee "这样在 "pr "后面出现1个到5个 "e "的字符串。或者 "/pre{,5}/ "代表pre出现0此到5次之间。
加号: "+ "字符用来匹配元字符前的字符出现一次或者多次。例如 "/ac+/ "表示被匹配的对象可以是 "act "、 "account "、 "acccc "等在 "a "后面出现一个或者多个 "c "的字符串。 "+ "相当于 "{1,} "。
星号: "* "字符用来匹配元字符前的字符出现零次或者多次。例如 "/ac*/ "表示被匹配的对象可以是 "app "、 "acp "、 "accp "等在 "a "后面出现零个或者多个 "c "的字符串。 "* "相当于 "{0,} "。
问号: "? "字符用来匹配元字符前的字符出现零次或者1次。例如 "/ac?/ "表示匹配的对象可以是 "a "、 "acp "、 "acwp "这样在 "a "后面出现零个或者1个 "c "的字符串。 "? "在正则表达式中还有一个非常重要的作用,即 "贪婪模式 "。

还有两个很重要的特殊字符就是 "[ ] "。他们可以匹配 "[] "之中出现过的字符,比如 "/[az]/ "可以匹配单个字符 "a "或者 "z ";如果把上面的表达式改成这样 "/[a-z]/ ",就可以匹配任何单个小写字母,比如 "a "、 "b "等等。
如果在 "[] "中出现了 "^ ",代表本表达式不匹配 "[] "内出现的字符,比如 "/[^a-z]/ "不匹配任何小写字母!并且正则表达式给出了几种 "[] "的默认值:
[:alpha:]:匹配任何字母
[:alnum:]:匹配任何字母和数字
[:digit:]:匹配任何数字
[:space:]:匹配空格符
[:upper:]:匹配任何大写字母
[:lower:]:匹配任何小写字母
[:punct:]:匹配任何标点符号
[:xdigit:]:匹配任何16进制数字

另外下面这些特殊字符在转义符号 "\ "转义后代表的含义如下:
s:匹配单个的空格符
S:用于匹配除单个空格符之外的所有字符。
d:用于匹配从0到9的数字,相当于 "/[0-9]/ "。
w:用于匹配字母,数字或下划线字符,相当于 "/[a-zA-Z0-9_]/ "。
W:用于匹配所有与w不匹配的字符,相当于 "/[^a-zA-Z0-9_]/ "。
D:用于匹配任何非10进制的数字字符。
.:用于匹配除换行符之外的所有字符,如果经过修饰符 "s "的修饰, ". "可以代表任意字符。

利用上面的特殊字符可以很方便的表达一些比较繁琐的模式匹配。例如 "/\d0000/ "利用上面的正则表达式可以匹配万以上,十万一下的整数字符串。

定位字符:
定位字符是正则表达式中又一类非常重要的字符,它的主要作用是用于对字符在匹配对象中的位置进行描述。
^:表示匹配的模式出现在匹配对象的开头(和在 "[] "里面不同)
$:表示匹配的模式出现在匹配对象的末尾
空格:表示匹配的模式出现在开始和结尾的两个边界之一
"/^he/ ":可以匹配以 "he "字符开头的字符串,比如hello、height等等;
"/he$/ ":可以匹配以 "he "字符结尾的字符串即she等;

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

deepseek web version entrance deepseek official website entrance deepseek web version entrance deepseek official website entrance Feb 19, 2025 pm 04:54 PM

DeepSeek is a powerful intelligent search and analysis tool that provides two access methods: web version and official website. The web version is convenient and efficient, and can be used without installation; the official website provides comprehensive product information, download resources and support services. Whether individuals or corporate users, they can easily obtain and analyze massive data through DeepSeek to improve work efficiency, assist decision-making and promote innovation.

How to install deepseek How to install deepseek Feb 19, 2025 pm 05:48 PM

There are many ways to install DeepSeek, including: compile from source (for experienced developers) using precompiled packages (for Windows users) using Docker containers (for most convenient, no need to worry about compatibility) No matter which method you choose, Please read the official documents carefully and prepare them fully to avoid unnecessary trouble.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

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

BITGet official website installation (2025 beginner's guide) BITGet official website installation (2025 beginner's guide) Feb 21, 2025 pm 08:42 PM

BITGet is a cryptocurrency exchange that provides a variety of trading services including spot trading, contract trading and derivatives. Founded in 2018, the exchange is headquartered in Singapore and is committed to providing users with a safe and reliable trading platform. BITGet offers a variety of trading pairs, including BTC/USDT, ETH/USDT and XRP/USDT. Additionally, the exchange has a reputation for security and liquidity and offers a variety of features such as premium order types, leveraged trading and 24/7 customer support.

Ouyi okx installation package is directly included Ouyi okx installation package is directly included Feb 21, 2025 pm 08:00 PM

Ouyi OKX, the world's leading digital asset exchange, has now launched an official installation package to provide a safe and convenient trading experience. The OKX installation package of Ouyi does not need to be accessed through a browser. It can directly install independent applications on the device, creating a stable and efficient trading platform for users. The installation process is simple and easy to understand. Users only need to download the latest version of the installation package and follow the prompts to complete the installation step by step.

See all articles