Regular expressions 1. Special characters
文章推薦指數: 80 %
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
延伸文章資訊
- 1Regular Expressions - Pages supplied by users
REGEX BASICS ; Matching simple expressions, Most characters match themselves. The only exceptions...
- 2What are ^.* and .*$ in regular expressions? - Stack Overflow
- 3Regex symbol list and regex examples - Codexpedia
Regex symbol list and regex examples ; \b Backslash and b, matches a word boundary. For example, ...
- 4116 - Stack Overflow
The regular expression for this is really simple. Just use a character class. The hyphen is a spe...
- 5Regular Expressions (REGEX): Basic symbols - Scripting Blog
Regular Expressions (REGEX): Basic symbols · Take special properties away from special characters...