Simple RegEx Tutorial
文章推薦指數: 80 %
Regular Expression can be used in Content Filter conditions. ... These symbols indicate the start and the end of a string, respectively: ... SimpleRegExTutorial RegularExpressioncanbeusedinContentFilterconditions. RegularExpressionscanbeextremelycomplexbuttheyareveryflexibleandpowerfulandcanbeusedtoperformcomparisonsthatcannotbedoneusingtheotherchecksavailable. Therefollowssomeverybasicexamplesofregularexpressionusage.Foracompletedescriptionpleasevisitwww.regular-expressions.info. ^'and'$' Firstofall,let'stakealookattwospecialsymbols:'^'and'$'.Thesesymbolsindicatethestartandtheendofastring,respectively: "^The" matchesanystringthatstartswith"The". "ofdespair$" matchesastringthatendsinwith"ofdespair". "^abc$" astringthatstartsandendswith"abc"-effectivelyanexactmatchcomparison. "notice" astringthathasthetext"notice"init. Youcanseethatifyoudon'tuseeitherofthesetwocharacters,you'resayingthatthepatternmayoccuranywhereinsidethestring--you'renot"hooking"ittoanyoftheedges. '*','+',and'?' Inaddition,thesymbols'*','+',and'?',denotethenumberoftimesacharacterorasequenceofcharactersmayoccur.Whattheymeanis:"zeroormore","oneormore",and"zeroorone."Herearesomeexamples: "ab*" matchesastringthathasanafollowedbyzeroormoreb's("ac","abc","abbc",etc.) "ab+" same,butthere'satleastoneb("abc","abbc",etc.,butnot"ac") "ab?" theremightbeasinglebornot("ac","abc"butnot"abbc"). "a?b+$" apossible'a'followedbyoneormore'b'sattheendofthestring: Matchesanystringendingwith"ab","abb","abbb"etc.or"b","bb"etc.butnot"aab","aabb"etc. Braces{} Youcanalsousebounds,whichappearinsidebracesandindicaterangesinthenumberofoccurrences: "ab{2}" matchesastringthathasanafollowedbyexactlytwob's("abb") "ab{2,}" thereareatleasttwob's("abb","abbbb",etc.) "ab{3,5}" fromthreetofiveb's("abbb","abbbb",or"abbbbb") Notethatyoumustalwaysspecifythefirstnumberofarange(i.e.,"{0,2}",not"{,2}").Also,asyoumighthavenoticed,thesymbols'*','+',and'?'havethesameeffectasusingthebounds"{0,}","{1,}",and"{0,1}",respectively. Now,toquantifyasequenceofcharacters,puttheminsideparentheses: "a(bc)*" matchesastringthathasanafollowedbyzeroormorecopiesofthesequence"bc" "a(bc){1,5}" onethroughfivecopiesof"bc." '|'ORoperator There'salsothe'|'symbol,whichworksasanORoperator: "hi|hello" matchesastringthathaseither"hi"or"hello"init "(b|cd)ef" astringthathaseither"bef"or"cdef" "(a|b)*c" astringthathasasequenceofalternatinga'sandb'sendinginac ('.') Aperiod('.')standsforanysinglecharacter: "a.[0-9]" matchesastringthathasanafollowedbyonecharacterandadigit "^.{3}$" astringwithexactly3characters Bracketexpressions specifywhichcharactersareallowedinasinglepositionofastring: "[ab]" matchesastringthathaseitheranaorab(that'sthesameas"a|b") "[a-d]" astringthathaslowercaseletters'a'through'd'(that'sequalto"a|b|c|d"andeven"[abcd]") "^[a-zA-Z]" astringthatstartswithaletter "[0-9]%" astringthathasasingledigitbeforeapercentsign ",[a-zA-Z0-9]$" astringthatendsinacommafollowedbyanalphanumericcharacter YoucanalsolistwhichcharactersyouDON'Twant--justusea'^'asthefirstsymbolinabracketexpression(i.e.,"%[^a-zA-Z]%"matchesastringwithacharacterthatisnotaletterbetweentwopercentsigns). Inordertobetakenliterally,youmustescapethecharacters"^.[$()|*+?{\"withabackslash('\'),astheyhavespecialmeaning.Ontopofthat,youmustescapethebackslashcharacteritselfinPHP3strings,so,forinstance,theregularexpression"(\$|A)[0-9]+"wouldhavethefunctioncall:ereg("(\\$|A)[0-9]+",$str)(whatstringdoesthatvalidate?) Justdon'tforgetthatbracketexpressionsareanexceptiontothatrule--insidethem,allspecialcharacters,includingthebackslash('\'),losetheirspecialpowers(i.e.,"[*\+?{}.]"matchesexactlyanyofthecharactersinsidethebrackets).And,astheregexmanualpagestellus:"Toincludealiteral']'inthelist,makeitthefirstcharacter(followingapossible'^').Toincludealiteral'-',makeitthefirstorlastcharacter,orthesecondendpointofarange." SeeAlsoSharedDatabaseSettingsAccessModeScheduleDomainAdminRightsSelectAccountsAccountOptions
延伸文章資訊
- 1Regular expressions 1. Special characters
The following characters are the meta characters that give special meaning to the regular express...
- 2Regular Expression (Regex) Tutorial
A range expression consists of two characters separated by a hyphen ( - ). It matches any single ...
- 3116 - Stack Overflow
The regular expression for this is really simple. Just use a character class. The hyphen is a spe...
- 4Regex symbol list and regex examples - Codexpedia
Regex symbol list and regex examples ; \b Backslash and b, matches a word boundary. For example, ...
- 5Regular Expressions - Pages supplied by users
REGEX BASICS ; Matching simple expressions, Most characters match themselves. The only exceptions...