Regular expressions 1. Special characters

文章推薦指數: 80 %
投票人數:10人

The following characters are the meta characters that give special meaning to the regular expression search syntax: \ the backslash escape character. Regularexpressions1.Specialcharacters Thefollowingcharactersarethemetacharactersthatgivespecialmeaningtotheregularexpressionsearchsyntax: \thebackslashescapecharacter. Thebackslashgivesspecialmeaningtothecharacterfollowingit.Forexample,thecombination"\n"standsforthenewline,oneofthecontrolcharacters.Thecombination"\w"standsfora"word"character,oneoftheconvenienceescapesequenceswhile"\1"isoneofthesubstitutionspecialcharacters.     Example:Theregex"aa\n"triestomatchtwoconsecutive"a"sattheendofaline,inclusivethenewlinecharacteritself.     Example:"a\+"matches"a+"andnotaseriesofoneor"a"s. ^thecaretistheanchorforthestartofthestring,orthenegationsymbol.     Example:"^a"matches"a"atthestartofthestring.     Example:"[^0-9]"matchesanynondigit. $thedollarsignistheanchorfortheendofthestring.     Example:"b$"matchesa"b"attheendofaline.     Example:"^$"matchestheemptystring. {}theopeningandclosingcurlybracketsareusedasrangequantifiers.     Example:"a{2,3}"matches"aa"or"aaa". []theopeningandclosingsquarebracketsdefineacharacterclasstomatchasinglecharacter. The"^"asthefirstcharacterfollowingthe"["negates,andthematchisforthecharactersnotlisted.The"-"denotesarangeofcharacters.Insidea"[]"characterclassconstruction,mostspecialcharactersareinterpretedasordinarycharacters.     Example:"[d-f]"isthesameas"[def]"andmatches"d","e"or"f".     Example:"[a-z]"matchesanylower-casecharactersinthealphabet.     Example:"[^0-9]"matchesanycharacterthatisnotanASCIIdigit.     Example:Asearchfor"[][()?<>$^.*?^]"inthestring"[]()?<>$^.*?^"followedbyareplacestring"r"hastheresult"rrrrrrrrrrrrr".Herethesearchstringisonecharacterclassandallthemetacharactersareinterpretedasordinarycharacterswithouttheneedtoescapethem. ()theopeningandclosingparenthes3sareusedforgroupingcharacters(orotherregexes). Thegroupscanbereferencedinboththesearchandthesubstitutionphase.Therealsoexistsomespecialconstructswithparentheses.     Example:"(ab)\1"matches"abab". .thedotmatchesanycharacterexceptthenewlinesymbol.     Example:".a"matchestwoconsecutivecharacterswherethelastoneis"a".     Example:".*\.txt$"matchesallstringsthatendin".txt". *theasteriskisthematch-zero-or-morequantifier.     Example:"^.*$"matchesanentireline. +theplussignisthematch-one-or-morequantifier. ?thequestionmarkisthematch-zero-or-onequantifier.Thequestionmarkisalsousedinspecialconstructswithparenthesesandinchangingmatchbehaviour. |theverticalpipeseparatesaseriesofalternatives.     Example:"(a|b|c)a"matches"aa"or"ba"or"ca". <>thesmallerandgreatersignsareanchorsthatspecifyaleftorrightwordboundary. -theminussignindicatesarangeinacharacterclass(whenitisnotatthefirstpositionafterthe"["openingbracketorthelastpositionbeforethe"]"closingbracket.     Example:"[A-Z]"matchesanyuppercasecharacter.     Example:"[A-Z-]"or"[-A-Z]"matchanyuppercasecharacteror"-". &theampersandisthe"substitutecompletematch"symbol. Linkstothispage Regularexpressions ©djmw,July18,2001



請為這篇文章評分?