Sie sind auf Seite 1von 3

Next:CommonCommands,Previous:Addresses,Up:sedPrograms

3.3OverviewofRegularExpressionSyntax
Toknowhowtousesed,peopleshouldunderstandregularexpressions(regexpforshort).Aregular
expressionisapatternthatismatchedagainstasubjectstringfromlefttoright.Mostcharactersare
ordinary:theystandforthemselvesinapattern,andmatchthecorrespondingcharactersinthesubject.
Asatrivialexample,thepattern
The quick brown fox
matchesaportionofasubjectstringthatisidenticaltoitself.Thepowerofregularexpressionscomes
fromtheabilitytoincludealternativesandrepetitionsinthepattern.Theseareencodedinthepatternby
theuseofspecialcharacters,whichdonotstandforthemselvesbutinsteadareinterpretedinsome
specialway.Hereisabriefdescriptionofregularexpressionsyntaxasusedinsed.
char
Asingleordinarycharactermatchesitself.
*
Matchesasequenceofzeroormoreinstancesofmatchesfortheprecedingregularexpression,
whichmustbeanordinarycharacter,aspecialcharacterprecededby\,a.,agroupedregexp
(seebelow),orabracketexpression.AsaGNUextension,apostfixedregularexpressioncan
alsobefollowedby*forexample,a**isequivalenttoa*.POSIX1003.12001saysthat*
standsforitselfwhenitappearsatthestartofaregularexpressionorsubexpression,butmany
nonGNUimplementationsdonotsupportthisandportablescriptsshouldinsteaduse\*inthese
contexts.
\+
As*,butmatchesoneormore.ItisaGNUextension.
\?
As*,butonlymatcheszeroorone.ItisaGNUextension.
\{i\}
As*,butmatchesexactlyisequences(iisadecimalintegerforportability,keepitbetween0and
255inclusive).
\{i,j\}
Matchesbetweeniandj,inclusive,sequences.
\{i,\}
Matchesmorethanorequaltoisequences.
\(regexp\)
Groupstheinnerregexpasawhole,thisisusedto:
Applypostfixoperators,like\(abcd\)*:thiswillsearchforzeroormorewholesequences
ofabcd,whileabcd*wouldsearchforabcfollowedbyzeroormoreoccurrencesof
d.Notethatsupportfor\(abcd\)*isrequiredbyPOSIX1003.12001,butmany
nonGNUimplementationsdonotsupportitandhenceitisnotuniversallyportable.
Usebackreferences(seebelow).
.
Matchesanycharacter,includingnewline.
^
Matchesthenullstringatbeginningofthepatternspace,i.e.whatappearsafterthecircumflex
mustappearatthebeginningofthepatternspace.
Inmostscripts,patternspaceisinitializedtothecontentofeachline(seeHowsedworks).So,it
isausefulsimplificationtothinkof^#includeasmatchingonlylineswhere#includeisthe
firstthingonlineiftherearespacesbefore,forexample,thematchfails.Thissimplificationis
validaslongastheoriginalcontentofpatternspaceisnotmodified,forexamplewithans
command.
^actsasaspecialcharacteronlyatthebeginningoftheregularexpressionorsubexpression(that
is,after\(or\|).Portablescriptsshouldavoid^atthebeginningofasubexpression,though,as
POSIXallowsimplementationsthattreat^asanordinarycharacterinthatcontext.
$
Itisthesameas^,butreferstoendofpatternspace.$alsoactsasaspecialcharacteronlyatthe
endoftheregularexpressionorsubexpression(thatis,before\)or\|),anditsuseattheendofa
subexpressionisnotportable.
[list]
[^list]
Matchesanysinglecharacterinlist:forexample,[aeiou]matchesallvowels.Alistmayinclude
sequenceslikechar1-char2,whichmatchesanycharacterbetween(inclusive)char1andchar2.
Aleading^reversesthemeaningoflist,sothatitmatchesanysinglecharacternotinlist.To
include]inthelist,makeitthefirstcharacter(afterthe^ifneeded),toinclude-inthelist,make
itthefirstorlasttoinclude^putitafterthefirstcharacter.
Thecharacters$,*,.,[,and\arenormallynotspecialwithinlist.Forexample,[\*]matches
either\or*,becausethe\isnotspecialhere.However,stringslike[.ch.],[=a=],and
[:space:]arespecialwithinlistandrepresentcollatingsymbols,equivalenceclasses,and
characterclasses,respectively,and[isthereforespecialwithinlistwhenitisfollowedby.,=,or
:.Also,whennotinPOSIXLY_CORRECTmode,specialescapeslike\nand\tarerecognized
withinlist.SeeEscapes.
regexp1\|regexp2
Matcheseitherregexp1orregexp2.Useparenthesestousecomplexalternativeregular
expressions.Thematchingprocesstrieseachalternativeinturn,fromlefttoright,andthefirstone
thatsucceedsisused.ItisaGNUextension.
regexp1regexp2
Matchestheconcatenationofregexp1andregexp2.Concatenationbindsmoretightlythan\|,^,
and$,butlesstightlythantheotherregularexpressionoperators.
\digit
Matchesthedigitth\(...\)parenthesizedsubexpressionintheregularexpression.Thisiscalled
abackreference.Subexpressionsareimplicitynumberedbycountingoccurrencesof\(leftto
right.
\n
Matchesthenewlinecharacter.
\char
Matcheschar,wherecharisoneof$,*,.,[,\,or^.NotethattheonlyClikebackslash
sequencesthatyoucanportablyassumetobeinterpretedare\nand\\inparticular\tisnot
portable,andmatchesatundermostimplementationsofsed,ratherthanatabcharacter.
Notethattheregularexpressionmatcherisgreedy,i.e.,matchesareattemptedfromlefttorightand,if
twoormorematchesarepossiblestartingatthesamecharacter,itselectsthelongest.
Examples:
abcdef
Matchesabcdef.
a*b
Matcheszeroormoreasfollowedbyasingleb.Forexample,boraaaaab.
a\?b
Matchesborab.
a\+b\+
Matchesoneormoreasfollowedbyoneormorebs:abistheshortestpossiblematch,but
otherexamplesareaaaaborabbbbboraaaaaabbbbbbb.
.*
.\+
Thesetwobothmatchallthecharactersinastringhowever,thefirstmatcheseverystring
(includingtheemptystring),whilethesecondmatchesonlystringscontainingatleastone
character.
^main.*(.*)
Thismatchesastringstartingwithmain,followedbyanopeningandclosingparenthesis.The
n,(and)neednotbeadjacent.
^#
Thismatchesastringbeginningwith#.
\\$
Thismatchesastringendingwithasinglebackslash.Theregexpcontainstwobackslashesfor
escaping.
\$
Instead,thismatchesastringconsistingofasingledollarsign,becauseitisescaped.
[a-zA-Z0-9]
IntheClocale,thismatchesanyASCIIlettersordigits.
[^ tab]\+
(Heretabstandsforasingletabcharacter.)Thismatchesastringofoneormorecharacters,none
ofwhichisaspaceoratab.Usuallythismeansaword.
^\(.*\)\n\1$
Thismatchesastringconsistingoftwoequalsubstringsseparatedbyanewline.
.\{9\}A$
ThismatchesninecharactersfollowedbyanA.
^.\{15\}A
Thismatchesthestartofastringthatcontains16characters,thelastofwhichisanA.

Das könnte Ihnen auch gefallen