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

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

EnableAppLockerviaGroupPolicybyopeninggpedit.msc,navigatingtoApplicationControlPolicies,creatingdefaultrules,andconfiguringruletypes;2.Createcustomrulesusingpublisher,path,orhashconditions,preferringpublisherrulesforsecurityandflexibility;3.Testrules

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.

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

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

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

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

RestartyourcomputerandroutertorefreshDHCPleasesandresetnetworkinterfaces.2.RuntheNetworktroubleshooterviaSettingstoautomaticallydetectandfixcommonissues.3.OpenCommandPromptasAdministratorandrunipconfig/release,ipconfig/renew,ipconfig/flushdns,netshwi
