常用正則表達(dá)式語法例句
Jun 21, 2016 am 09:15 AM語法|正則
常用正則表達(dá)式語法例句
這里有一些可能會(huì)遇到的正則表達(dá)式示例:
/^\[ \t]*$/ "^\[ \t]*$" 匹配一個(gè)空白行。
/\d{2}-\d{5}/ "\d{2}-\d{5}" 驗(yàn)證一個(gè)ID號(hào)碼是否由一個(gè)2位字,一
個(gè)連字符以及一個(gè)5位數(shù)字組成。
/.*/ ".*" 匹配一個(gè) HTML 標(biāo)記。
下表是元字符及其在正則表達(dá)式上下文中的行為的一個(gè)完整列表:
字符 描述
\ 將下一個(gè)字符標(biāo)記為一個(gè)特殊字符、或一個(gè)原義字符、或一個(gè) 后
向引用、或一個(gè)八進(jìn)制轉(zhuǎn)義符。例如,’n’ 匹配字符 "n"。’\n’
匹配一個(gè)換行符。序列 ’\\’ 匹配 "\" 而 "\(" 則匹配 "("。
^ 匹配輸入字符串的開始位置。如果設(shè)置了 RegExp 對(duì)象的
Multiline 屬性,^ 也匹配 ’\n’ 或 ’\r’ 之后的位置。
$ 匹配輸入字符串的結(jié)束位置。如果設(shè)置了 RegExp 對(duì)象的
Multiline 屬性,$ 也匹配 ’\n’ 或 ’\r’ 之前的位置。
* 匹配前面的子表達(dá)式零次或多次。例如,zo* 能匹配 "z" 以及
"zoo"。 * 等價(jià)于{0,}。
+ 匹配前面的子表達(dá)式一次或多次。例如,’zo+’ 能匹配 "zo" 以
及 "zoo",但不能匹配 "z"。+ 等價(jià)于 {1,}。
? 匹配前面的子表達(dá)式零次或一次。例如,"do(es)?" 可以匹配
"do" 或 "does" 中的"do" 。? 等價(jià)于 {0,1}。
{n} n 是一個(gè)非負(fù)整數(shù)。匹配確定的 n 次。例如,’o{2}’ 不能匹配
"Bob" 中的 ’o’,但是能匹配 "food" 中的兩個(gè) o。
{n,} n 是一個(gè)非負(fù)整數(shù)。至少匹配n 次。例如,’o{2,}’ 不能匹配
"Bob" 中的 ’o’,但能匹配 "foooood" 中的所有 o?!痮{1,}’
等價(jià)于 ’o+’。’o{0,}’ 則等價(jià)于 ’o*’。
{n,m} m 和 n 均為非負(fù)整數(shù),其中n 配 m 次。劉, "o{1,3}" 將匹配 "fooooood" 中的前三個(gè)o。
’o{0,1}’等價(jià)于’o?’。請(qǐng)注意在逗號(hào)和兩個(gè)數(shù)之間不能有空格
? 當(dāng)該字符緊跟在任何一個(gè)其他限制符 (*, +, ?, {n}, {n,},
{n,m}) 后面時(shí),匹配模式是非貪婪的。非貪婪模式盡可能少的
匹配所搜索的字符串,而默認(rèn)的貪婪模式則盡可能多的匹配所搜
索的字符串。例如,對(duì)于字符串 "oooo",’o+?’ 將匹配單個(gè)
"o",而 ’o+’ 將匹配所有 ’o’。
. 匹配除 "\n" 之外的任何單個(gè)字符。要匹配包括 ’\n’ 在內(nèi)的任
何字符,請(qǐng)使用象 ’[.\n]’ 的模式。
(pattern) 匹配pattern 并獲取這一匹配。所獲取的匹配可以從產(chǎn)生的
Matches 集合得到,在VBScript 中使用 SubMatches 集合,在
Visual Basic Scripting Edition 中則使用 $0…$9 屬性。要
匹配圓括號(hào)字符,請(qǐng)使用 ’\(’ 或 ’\)’。
(?:pattern) 匹配 pattern 但不獲取匹配結(jié)果,也就是說這是一個(gè)非獲取匹
配,不進(jìn)行存儲(chǔ)供以后使用。這在使用 "或" 字符 (|) 來組合
一個(gè)模式的各個(gè)部分是很有用。例如, ’industr(?:y|ies) 就
是一個(gè)比 ’industry|industries’ 更簡略的表達(dá)式。
(?=pattern) 正向預(yù)查,在任何匹配 pattern 的字符串開始處匹配查找字符
串。這是一個(gè)非獲取匹配,也就是說,該匹配不需要獲取供以后
使用。例如,’Windows (?=95|98|NT|2000)’ 能匹配"Windows
2000"中的"Windows",但不能匹配"Windows3 .1"中"Windows"。
預(yù)查不消耗字符,也就是說,在一個(gè)匹配發(fā)生后,在最后一次匹
配之后立即開始下一次匹配的搜索,而不是從包含預(yù)查的字符之
后開始。
(?!pattern) 負(fù)向預(yù)查,在任何不匹配Negative lookahead matches the
search string at any point where a string not matching
pattern 的字符串開始處匹配查找字符串。這是一個(gè)非獲取匹
配,也就是說,該匹配不需要獲取供以后使用。例如’Windows
(?!95|98|NT|2000)’ 能匹配 "Windows 3.1" 中的 "Windows",
但不能匹配 "Windows 2000" 中的 "Windows"。預(yù)查不消耗字
符,也就是說,在一個(gè)匹配發(fā)生后,在最后一次匹配之后立即開
始下一次匹配的搜索,而不是從包含預(yù)查的字符之后開始
x|y 匹配 x 或 y。例如,’z|food’ 能匹配 "z" 或 "food"。’(z|f)
ood’ 則匹配 "zood" 或 "food"。
[xyz] 字符集合。匹配所包含的任意一個(gè)字符。例如, ’[abc]’ 可以
匹配 "plain" 中的 ’a’。
[^xyz] 負(fù)值字符集合。匹配未包含的任意字符。例如, ’[^abc]’ 可以
匹配 "plain" 中的’p’。
[a-z] 字符范圍。匹配指定范圍內(nèi)的任意字符。例如,’[a-z]’ 可以匹
配 ’a’ 到 ’z’ 范圍內(nèi)的任意小寫字母字符。
[^a-z] 負(fù)值字符范圍。匹配任何不在指定范圍內(nèi)的任意字符。例如,
’[^a-z]’ 可以匹配任何不在 ’a’ 到 ’z’ 范圍內(nèi)的任意字符。
\b 匹配一個(gè)單詞邊界,也就是指單詞和空格間的位置。例如,
’er\b’ 可以匹配"never" 中的 ’er’,但不能匹配 "verb" 中
的 ’er’。
\B 匹配非單詞邊界?!痚r\B’ 能匹配 "verb" 中的 ’er’,但不能匹
配 "never" 中的 ’er’。
\cx 匹配由x指明的控制字符。例如, \cM 匹配一個(gè) Control-M 或
回車符。 x 的值必須為 A-Z 或 a-z 之一。否則,將 c 視為一
個(gè)原義的 ’c’ 字符。
\d 匹配一個(gè)數(shù)字字符。等價(jià)于 [0-9]。
\D 匹配一個(gè)非數(shù)字字符。等價(jià)于 [^0-9]。
\f 匹配一個(gè)換頁符。等價(jià)于 \x0c 和 \cL。
\n 匹配一個(gè)換行符。等價(jià)于 \x0a 和 \cJ。
\r 匹配一個(gè)回車符。等價(jià)于 \x0d 和 \cM。
\s 匹配任何空白字符,包括空格、制表符、換頁符等等。等價(jià)于
[ \f\n\r\t\v]。
\S 匹配任何非空白字符。等價(jià)于 [^ \f\n\r\t\v]。
\t 匹配一個(gè)制表符。等價(jià)于 \x09 和 \cI。
\v 匹配一個(gè)垂直制表符。等價(jià)于 \x0b 和 \cK。
\w 匹配包括下劃線的任何單詞字符。等價(jià)于’[A-Za-z0-9_]’。
\W 匹配任何非單詞字符。等價(jià)于 ’[^A-Za-z0-9_]’。
\xn 匹配 n,其中 n 為十六進(jìn)制轉(zhuǎn)義值。十六進(jìn)制轉(zhuǎn)義值必須為確
定的兩個(gè)數(shù)字長。例如, ’\x41’ 匹配 "A"?!痋x041’ 則等價(jià)
于 ’\x04’ & "1"。正則表達(dá)式中可以使用 ASCII 編碼。.
\num 匹配 num,其中num是一個(gè)正整數(shù)。對(duì)所獲取的匹配的引用。
例如,’(.)\1’ 匹配兩個(gè)連續(xù)的相同字符。
\n 標(biāo)識(shí)一個(gè)八進(jìn)制轉(zhuǎn)義值或一個(gè)后向引用。如果 \n 之前至少 n
個(gè)獲取的子表達(dá)式,則 n 為后向引用。否則,如果 n 為八進(jìn)制
數(shù)字 (0-7),則 n 為一個(gè)八進(jìn)制轉(zhuǎn)義值。
\nm 標(biāo)識(shí)一個(gè)八進(jìn)制轉(zhuǎn)義值或一個(gè)后向引用。如果 \nm 之前至少有
is preceded by at least nm 個(gè)獲取得子表達(dá)式,則 nm 為后
向引用。如果 \nm 之前至少有 n 個(gè)獲取,則 n 為一個(gè)后跟文
字 m 的后向引用。如果前面的條件都不滿足,若 n 和 m 均為
八進(jìn)制數(shù)字 (0-7),則 \nm 將匹配八進(jìn)制轉(zhuǎn)義值 nm。
\nml 如果 n 為八進(jìn)制數(shù)字 (0-3),且 m 和 l 均為八進(jìn)制數(shù)字 (0-
7),則匹配八進(jìn)制轉(zhuǎn)義值 nml。
\un 匹配 n,其中 n 是一個(gè)用四個(gè)十六進(jìn)制數(shù)字表示的Unicode字
符。例如, \u00A9 匹配版權(quán)符號(hào) (?)。

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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)

ToresolvenetworkconnectivityissuesinWindows,resettheTCP/IPstackbyfirstopeningCommandPromptasAdministrator,thenrunningthecommandnetshintipreset,andfinallyrestartingyourcomputertoapplychanges;ifissuespersist,optionallyrunnetshwinsockresetandrebootagain

Linux is suitable for old hardware, has high security and is customizable, but has weak software compatibility; Windows software is rich and easy to use, but has high resource utilization. 1. In terms of performance, Linux is lightweight and efficient, suitable for old devices; Windows has high hardware requirements. 2. In terms of software, Windows has wider compatibility, especially professional tools and games; Linux needs to use tools to run some software. 3. In terms of security, Linux permission management is stricter and updates are convenient; although Windows is protected, it is still vulnerable to attacks. 4. In terms of difficulty of use, the Linux learning curve is steep; Windows operation is intuitive. Choose according to requirements: choose Linux with performance and security, and choose Windows with compatibility and ease of use.

Hyper-VcanbeenabledonWindowsPro,Enterprise,orEducationeditionsbymeetingsystemrequirementsincluding64-bitCPUwithSLAT,VMMonitorModeExtension,BIOS/UEFIvirtualizationenabled,andatleast4GBRAM.2.EnableHyper-VviaWindowsFeaturesbyopeningoptionalfeatures,chec

Right-clickthedesktopandselect"Displaysettings"toopenthedisplayoptions.2.Underthe"Display"section,clickthe"Displayresolution"dropdownandchoosearesolution,preferablytherecommendedoneforbestimagequality.3.Confirmthechanges

Checkifthetouchpadisdisabledbyusingthefunctionkey(Fn F6/F9/F12),adedicatedtogglebutton,orensuringit’sturnedoninSettings>Devices>Touchpad,andunplugexternalmice.2.UpdateorreinstallthetouchpaddriverviaDeviceManagerbyselectingUpdatedriverorUninstal

VerifytheWindowsISOisfromMicrosoftandrecreatethebootableUSBusingtheMediaCreationToolorRufuswithcorrectsettings;2.Ensurehardwaremeetsrequirements,testRAMandstoragehealth,anddisconnectunnecessaryperipherals;3.ConfirmBIOS/UEFIsettingsmatchtheinstallatio

Windowsdoesnotnativelysupportpermanentlysavingprocessoraffinityforapplications,butyoucanachievepersistentaffinityusingscriptsorthird-partytools.1.ProcessoraffinitybindsaprocesstospecificCPUcores,improvingperformanceorthermalmanagement,thoughWindowsre

OpenDeviceManagerbypressingWin XandselectingitorsearchingintheStartmenu.2.Locatetheproblematicdevice—suchasDisplayadapters,Soundvideoandgamecontrollers,Networkadapters,orinputdevices—right-clickitandselectProperties.3.GototheDrivertabandclick“RollBac
