亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home php教程 php手冊(cè) 轉(zhuǎn)載www.phpuser.com中關(guān)于正則表達(dá)式的文章,講的非常詳細(xì)

轉(zhuǎn)載www.phpuser.com中關(guān)于正則表達(dá)式的文章,講的非常詳細(xì)

Jun 21, 2016 am 09:12 AM
email once php quot

正則

PHP中的正規(guī)表達(dá)式(一)
Hunte 2000年4月14日

把它EMAIL給我 我來(lái)談?wù)勎业目捶?上一篇 下一篇




PHP繼承*NIX的一貫傳統(tǒng),完全支持正規(guī)表達(dá)式的處理。正規(guī)表達(dá)式提供了一種高級(jí)的,但不直觀的字符串匹配和處理的方法。用過(guò)PERL的正規(guī)表達(dá)式的朋友都知道,正規(guī)表達(dá)式的功能非常強(qiáng)大,但學(xué)起來(lái)不是那么容易。
比如:

^.+@.+\\..+$

這段有效卻難以理解的代碼足夠使一些程序員頭痛(我就是)或者讓他們放棄使用正規(guī)表達(dá)式。相信當(dāng)你讀完這個(gè)教程后,就可以理解這段代碼的含義了。

基本模式匹配

一切從最基本的開始。模式,是正規(guī)表達(dá)式最基本的元素,它們是一組描述字符串特征的字符。模式可以很簡(jiǎn)單,由普通的字符串組成,也可以非常復(fù)雜,往往用特殊的字符表示一個(gè)范圍內(nèi)的字符、重復(fù)出現(xiàn),或表示上下文。例如:

^once

這個(gè)模式包含一個(gè)特殊的字符^,表示該模式只匹配那些以once開頭的字符串。例如該模式與字符串"once upon a time"匹配,與"There once was a man from NewYork"不匹配。正如如^符號(hào)表示開頭一樣,$符號(hào)用來(lái)匹配那些以給定模式結(jié)尾的字符串。

bucket$

這個(gè)模式與"Who kept all of this cash in a bucket"匹配,與"buckets"不匹配。字符^和$同時(shí)使用時(shí),表示精確匹配(字符串與模式一樣)。例如:

^bucket$

只匹配字符串"bucket"。如果一個(gè)模式不包括^和$,那么它與任何包含該模式的字符串匹配。例如:模式

once

與字符串

There once was a man from NewYork
Who kept all of his cash in a bucket.

是匹配的。

在該模式中的字母(o-n-c-e)是字面的字符,也就是說(shuō),他們表示該字母本身,數(shù)字也是一樣的。其他一些稍微復(fù)雜的字符,如標(biāo)點(diǎn)符號(hào)和白字符(空格、制表符等),要用到轉(zhuǎn)義序列。所有的轉(zhuǎn)義序列都用反斜杠(\)打頭。制表符的轉(zhuǎn)義序列是:\t。所以如果我們要檢測(cè)一個(gè)字符串是否以制表符開頭,可以用這個(gè)模式:

^\t

類似的,用\n表示“新行”,\r表示回車。其他的特殊符號(hào),可以用在前面加上反斜杠,如反斜杠本身用\\表示,句號(hào).用\.表示,以此類推。

字符簇

在INTERNET的程序中,正規(guī)表達(dá)式通常用來(lái)驗(yàn)證用戶的輸入。當(dāng)用戶提交一個(gè)FORM以后,要判斷輸入的電話號(hào)碼、地址、EMAIL地址、信用卡號(hào)碼等是否有效,用普通的基于字面的字符是不夠的。

所以要用一種更自由的描述我們要的模式的辦法,它就是字符簇。要建立一個(gè)表示所有元音字符的字符簇,就把所有的元音字符放在一個(gè)方括號(hào)里:

[AaEeIiOoUu]

這個(gè)模式與任何元音字符匹配,但只能表示一個(gè)字符。用連字號(hào)可以表示一個(gè)字符的范圍,如:

[a-z] //匹配所有的小寫字母
[A-Z] //匹配所有的大寫字母
[a-zA-Z] //匹配所有的字母
[0-9] //匹配所有的數(shù)字
[0-9\.\-] //匹配所有的數(shù)字,句號(hào)和減號(hào)
[ \f\r\t\n] //匹配所有的白字符

同樣的,這些也只表示一個(gè)字符,這是一個(gè)非常重要的。如果要匹配一個(gè)由一個(gè)小寫字母和一位數(shù)字組成的字符串,比如"z2"、"t6"或"g7",但不是"ab2"、"r2d3" 或"b52"的話,用這個(gè)模式:

^[a-z][0-9]$

盡管[a-z]代表26個(gè)字母的范圍,但在這里它只能與第一個(gè)字符是小寫字母的字符串匹配。

前面曾經(jīng)提到^表示字符串的開頭,但它還有另外一個(gè)含義。當(dāng)在一組方括號(hào)里使用^是,它表示“非”或“排除”的意思,常常用來(lái)剔除某個(gè)字符。還用前面的例子,我們要求第一個(gè)字符不能是數(shù)字:

^[^0-9][0-9]$

這個(gè)模式與"&5"、"g7"及"-2"是匹配的,但與"12"、"66"是不匹配的。下面是幾個(gè)排除特定字符的例子:

[^a-z] //除了小寫字母以外的所有字符
[^\\\/\^] //除了(\)(/)(^)之外的所有字符
[^\"\'] //除了雙引號(hào)(")和單引號(hào)(')之外的所有字符


特殊字符"." (點(diǎn),句號(hào))在正規(guī)表達(dá)式中用來(lái)表示除了“新行”之外的所有字符。所以模式"^.5$"與任何兩個(gè)字符的、以數(shù)字5結(jié)尾和以其他非“新行”字符開頭的字符串匹配。模式"."可以匹配任何字符串,除了空串和只包括一個(gè)“新行”的字符串。

PHP的正規(guī)表達(dá)式有一些內(nèi)置的通用字符簇,列表如下:

字符簇 含義
[[:alpha:]] 任何字母
[[:digit:]] 任何數(shù)字
[[:alnum:]] 任何字母和數(shù)字
[[:space:]] 任何白字符
[[:upper:]] 任何大寫字母
[[:lower:]] 任何小寫字母
[[:punct:]] 任何標(biāo)點(diǎn)符號(hào)
[[:xdigit:]] 任何16進(jìn)制的數(shù)字,相當(dāng)于[0-9a-fA-F]



PHP中的正規(guī)表達(dá)式(二)
Hunte 2000年4月17日

把它EMAIL給我 我來(lái)談?wù)勎业目捶?上一篇 下一篇




確定重復(fù)出現(xiàn)

到現(xiàn)在為止,你已經(jīng)知道如何去匹配一個(gè)字母或數(shù)字,但更多的情況下,可能要匹配一個(gè)單詞或一組數(shù)字。一個(gè)單詞有若干個(gè)字母組成,一組數(shù)字有若干個(gè)單數(shù)組成。跟在字符或字符簇后面的花括號(hào)({})用來(lái)確定前面的內(nèi)容的重復(fù)出現(xiàn)的次數(shù)。

字符簇 含義
^[a-zA-Z_]$ 所有的字母和下劃線
^[[:alpha:]]{3}$ 所有的3個(gè)字母的單詞
^a$ 字母a
^a{4}$ aaaa
^a{2,4}$ aa,aaa或aaaa
^a{1,3}$ a,aa或aaa
^a{2,}$ 包含多于兩個(gè)a的字符串
^a{2,} 如:aardvark和aaab,但apple不行
a{2,} 如:baad和aaa,但Nantucket不行
\t{2} 兩個(gè)制表符
.{2} 所有的兩個(gè)字符

這些例子描述了花括號(hào)的三種不同的用法。一個(gè)數(shù)字,{x}的意思是“前面的字符或字符簇只出現(xiàn)x次”;一個(gè)數(shù)字加逗號(hào),{x,}的意思是“前面的內(nèi)容出現(xiàn)x或更多的次數(shù)”;兩個(gè)用逗號(hào)分隔的數(shù)字,{x,y}表示“前面的內(nèi)容至少出現(xiàn)x次,但不超過(guò)y次”。我們可以把模式擴(kuò)展到更多的單詞或數(shù)字:

^[a-zA-Z0-9_]{1,}$ //所有包含一個(gè)以上的字母、數(shù)字或下劃線的字符串
^[0-9]{1,}$ //所有的正數(shù)
^\-{0,1}[0-9]{1,}$ //所有的整數(shù)
^\-{0,1}[0-9]{0,}\.{0,1}[0-9]{0,}$ //所有的小數(shù)

最后一個(gè)例子不太好理解,是嗎?這么看吧:與所有以一個(gè)可選的負(fù)號(hào)(\-{0,1})開頭(^)、跟著0個(gè)或更多的數(shù)字([0-9]{0,})、和一個(gè)可選的小數(shù)點(diǎn)(\.{0,1})再跟上0個(gè)或多個(gè)數(shù)字([0-9]{0,}),并且沒有其他任何東西($)。下面你將知道能夠使用的更為簡(jiǎn)單的方法。

特殊字符"?"與{0,1}是相等的,它們都代表著:“0個(gè)或1個(gè)前面的內(nèi)容”或“前面的內(nèi)容是可選的”。所以剛才的例子可以簡(jiǎn)化為:

^\-?[0-9]{0,}\.?[0-9]{0,}$

特殊字符"*"與{0,}是相等的,它們都代表著“0個(gè)或多個(gè)前面的內(nèi)容”。最后,字符"+"與 {1,}是相等的,表示“1個(gè)或多個(gè)前面的內(nèi)容”,所以上面的4個(gè)例子可以寫成:

^[a-zA-Z0-9_]+$ //所有包含一個(gè)以上的字母、數(shù)字或下劃線的字符串
^[0-9]+$ //所有的正數(shù)
^\-?[0-9]+$ //所有的整數(shù)
^\-?[0-9]*\.?[0-9]*$ //所有的小數(shù)

當(dāng)然這并不能從技術(shù)上降低正規(guī)表達(dá)式的復(fù)雜性,但可以使它們更容易閱讀。




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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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)

Hot Topics

PHP Tutorial
1488
72
PHP calls AI intelligent voice assistant PHP voice interaction system construction PHP calls AI intelligent voice assistant PHP voice interaction system construction Jul 25, 2025 pm 08:45 PM

User voice input is captured and sent to the PHP backend through the MediaRecorder API of the front-end JavaScript; 2. PHP saves the audio as a temporary file and calls STTAPI (such as Google or Baidu voice recognition) to convert it into text; 3. PHP sends the text to an AI service (such as OpenAIGPT) to obtain intelligent reply; 4. PHP then calls TTSAPI (such as Baidu or Google voice synthesis) to convert the reply to a voice file; 5. PHP streams the voice file back to the front-end to play, completing interaction. The entire process is dominated by PHP to ensure seamless connection between all links.

How to use PHP to build social sharing functions PHP sharing interface integration practice How to use PHP to build social sharing functions PHP sharing interface integration practice Jul 25, 2025 pm 08:51 PM

The core method of building social sharing functions in PHP is to dynamically generate sharing links that meet the requirements of each platform. 1. First get the current page or specified URL and article information; 2. Use urlencode to encode the parameters; 3. Splice and generate sharing links according to the protocols of each platform; 4. Display links on the front end for users to click and share; 5. Dynamically generate OG tags on the page to optimize sharing content display; 6. Be sure to escape user input to prevent XSS attacks. This method does not require complex authentication, has low maintenance costs, and is suitable for most content sharing needs.

How to use PHP combined with AI to achieve text error correction PHP syntax detection and optimization How to use PHP combined with AI to achieve text error correction PHP syntax detection and optimization Jul 25, 2025 pm 08:57 PM

To realize text error correction and syntax optimization with AI, you need to follow the following steps: 1. Select a suitable AI model or API, such as Baidu, Tencent API or open source NLP library; 2. Call the API through PHP's curl or Guzzle and process the return results; 3. Display error correction information in the application and allow users to choose whether to adopt it; 4. Use php-l and PHP_CodeSniffer for syntax detection and code optimization; 5. Continuously collect feedback and update the model or rules to improve the effect. When choosing AIAPI, focus on evaluating accuracy, response speed, price and support for PHP. Code optimization should follow PSR specifications, use cache reasonably, avoid circular queries, review code regularly, and use X

PHP creates a blog comment system to monetize PHP comment review and anti-brush strategy PHP creates a blog comment system to monetize PHP comment review and anti-brush strategy Jul 25, 2025 pm 08:27 PM

1. Maximizing the commercial value of the comment system requires combining native advertising precise delivery, user paid value-added services (such as uploading pictures, top-up comments), influence incentive mechanism based on comment quality, and compliance anonymous data insight monetization; 2. The audit strategy should adopt a combination of pre-audit dynamic keyword filtering and user reporting mechanisms, supplemented by comment quality rating to achieve content hierarchical exposure; 3. Anti-brushing requires the construction of multi-layer defense: reCAPTCHAv3 sensorless verification, Honeypot honeypot field recognition robot, IP and timestamp frequency limit prevents watering, and content pattern recognition marks suspicious comments, and continuously iterate to deal with attacks.

How to use PHP to combine AI to generate image. PHP automatically generates art works How to use PHP to combine AI to generate image. PHP automatically generates art works Jul 25, 2025 pm 07:21 PM

PHP does not directly perform AI image processing, but integrates through APIs, because it is good at web development rather than computing-intensive tasks. API integration can achieve professional division of labor, reduce costs, and improve efficiency; 2. Integrating key technologies include using Guzzle or cURL to send HTTP requests, JSON data encoding and decoding, API key security authentication, asynchronous queue processing time-consuming tasks, robust error handling and retry mechanism, image storage and display; 3. Common challenges include API cost out of control, uncontrollable generation results, poor user experience, security risks and difficult data management. The response strategies are setting user quotas and caches, providing propt guidance and multi-picture selection, asynchronous notifications and progress prompts, key environment variable storage and content audit, and cloud storage.

PHP realizes commodity inventory management and monetization PHP inventory synchronization and alarm mechanism PHP realizes commodity inventory management and monetization PHP inventory synchronization and alarm mechanism Jul 25, 2025 pm 08:30 PM

PHP ensures inventory deduction atomicity through database transactions and FORUPDATE row locks to prevent high concurrent overselling; 2. Multi-platform inventory consistency depends on centralized management and event-driven synchronization, combining API/Webhook notifications and message queues to ensure reliable data transmission; 3. The alarm mechanism should set low inventory, zero/negative inventory, unsalable sales, replenishment cycles and abnormal fluctuations strategies in different scenarios, and select DingTalk, SMS or Email Responsible Persons according to the urgency, and the alarm information must be complete and clear to achieve business adaptation and rapid response.

Beyond the LAMP Stack: PHP's Role in Modern Enterprise Architecture Beyond the LAMP Stack: PHP's Role in Modern Enterprise Architecture Jul 27, 2025 am 04:31 AM

PHPisstillrelevantinmodernenterpriseenvironments.1.ModernPHP(7.xand8.x)offersperformancegains,stricttyping,JITcompilation,andmodernsyntax,makingitsuitableforlarge-scaleapplications.2.PHPintegrateseffectivelyinhybridarchitectures,servingasanAPIgateway

PHP integrated AI speech recognition and translator PHP meeting record automatic generation solution PHP integrated AI speech recognition and translator PHP meeting record automatic generation solution Jul 25, 2025 pm 07:06 PM

Select the appropriate AI voice recognition service and integrate PHPSDK; 2. Use PHP to call ffmpeg to convert recordings into API-required formats (such as wav); 3. Upload files to cloud storage and call API asynchronous recognition; 4. Analyze JSON results and organize text using NLP technology; 5. Generate Word or Markdown documents to complete the automation of meeting records. The entire process needs to ensure data encryption, access control and compliance to ensure privacy and security.

See all articles