Академический Документы
Профессиональный Документы
Культура Документы
PreviousChapter
NextChapter
Perl5byExample:RegularExpressions
10RegularExpressions
Youcanusearegularexpressiontofindpatternsinstrings:forexample,tolook
foraspecificnameinaphonelistorallofthenamesthatstartwiththelettera.
PatternmatchingisoneofPerl'smostpowerfulandprobablyleastunderstood
features.Butafteryoureadthischapter,you'llbeabletohandleregular
expressionsalmostaswellasaPerlguru.Withalittlepractice,you'llbeableto
dosomeincrediblyhandythings.
TherearethreemainusesforregularexpressionsinPerl:matching,substitution,
andtranslation.Thematchingoperationusesthem//operator,whichevaluates
toatrueorfalsevalue.Thesubstitutionoperationsubstitutesoneexpressionfor
anotheritusesthes///operator.Thetranslationoperationtranslatesonesetof
characterstoanotherandusesthetr///operator.Theseoperatorsare
summarizedinTable10.1.
Table10.1Perl'sRegularExpressionOperators
Links
Sections
Chapters
Copyright
Sections
Operator
Description
m/PATTERN/
Thisoperatorreturnstrueif
PATTERNisfoundin$_.
s/PATTERN/REPLACEMENT/
Thisoperatorreplacesthesub
stringmatchedbyPATTERN
withREPLACEMENT.
tr/CHARACTERS/REPLACEMENTS/
Thisoperatorreplaces
charactersspecifiedby
CHARACTERSwiththe
charactersin
REPLACEMENTS.
PatternDelimiters
TheMatching
Operator(m//)
TheSubstitution
Operator(s///)
TheTranslation
Operator(tr///)
TheBindingOperators
(=~and!~)
Allthreeregularexpressionoperatorsworkwith$_asthestringtosearch.You
canusethebindingoperators(seethesection"TheBindingOperators(=~and
HowtoCreatePatterns !~)"laterinthissection)tosearchavariableotherthan$_.
PatternExamples
Summary
ReviewQuestions
ReviewExercises
Chapters
Boththematching(m//)andthesubstitution(s///)operatorsperformvariable
interpolationonthePATTERNandREPLACEMENTstrings.Thiscomesinhandyif
youneedtoreadthepatternfromthekeyboardorafile.
Ifthematchpatternevaluatestotheemptystring,thelastvalidpatternisused.
So,ifyouseeastatementlikeprintif//;inaPerlprogram,lookforthe
previousregularexpressionoperatortoseewhatthepatternreallyis.The
substitutionoperatoralsousesthisinterpretationoftheemptypattern.
ERRATA
http://affy.blogspot.my/p5be/ch10.htm
1/30
11/6/2015
Perl5byExample:RegularExpressions
Inthischapter,youlearnaboutpatterndelimitersandthenabouteachtypeof
regularexpressionoperator.Afterthat,youlearnhowtocreatepatternsinthe
section"HowtoCreatePatterns"..Then,the"PatternExamples"sectionshows
yousomesituationsandhowregularexpressionscanbeusedtoresolvethe
situations.
Welcome!
Introduction
PartI:BasicPerl
01GettingYourFeetWet
02NumericandStringLiterals
03Variables
04Operators
05Functions
06Statements
07ControlStatements
08References
PatternDelimiters
Everyregularexpressionoperatorallowstheuseofalternativepattern
delimiters.Adelimitermarksthebeginningandendofagivenpattern.Inthe
followingstatement,
PartII:IntermediatePerl
m//;
09UsingFiles
youseetwoofthestandarddelimiterstheslashes(//).However,youcanuse
anycharacterasthedelimiter.Thisfeatureisusefulifyouwanttousetheslash
characterinsideyourpattern.Forinstance,tomatchafileyouwouldnormally
use:
10RegularExpressions
11CreatingReports
PartIII:AdvancedPerl
12UsingSpecialVariables
13HandlingErrorsand
Signals
14WhatAreObjects?
15PerlModules
16DebuggingPerl
17CommandlineOptions
PartIV:PerlandtheInternet
18UsingInternetProtocols
ftplib.pl
19WhatisCGI?
20FormProcessing
21UsingPerlwithWeb
Servers
22InternetResources
Appendixes
AReviewQuestions
BGlossary
CFunctionList
DTheWindowsRegistry
EWhat'sOntheCD?
m/\/root\/home\/random.dat/
Thismatchstatementishardtoreadbecausealloftheslashesseemtorun
together(someprogrammerssaytheylookliketeepees).Ifyouuseanalternate
delimiter,ifmightlooklikethis:
m!/root/home/random.dat!
or
m{/root/home/random.dat}
Youcanseethattheseexamplesarealittleclearer.Thelastexamplealsoshows
thatifaleftbracketisusedasthestartingdelimiter,thentheendingdelimiter
mustbetherightbracket.
ErrataNote
Theprintedversionofthisbookshowstheaboveexamplesas
m!\/root\/home\/random.dat!andasm{\/root\/home\/random.dat}.WhileI
waswritingthebookitdidnotoccurtobethatthe/characterwasnota
metacharacterandonlyneededtobeescapedbecauseofthedelimiters.
Obviously,ifthe/characteristhedelimiter,itneedstobeescapedinorderto
useitinsidethepattern.However,ifanalternativedelimiterisused,itnolonger
needstobeescaped.thisfactwaspointedouttomebyGarenDeve.
Boththematchandsubstitutionoperatorsletyouusevariableinterpolation.Youcantakeadvantage
ofthistouseasinglequotedstringthatdoesnotrequiretheslashtobeescaped.Forinstance:
$file='/root/home/random.dat';
m/$file/;
Youmightfindthatthistechniqueyieldsclearercodethansimplychangingthedelimiters.
Ifyouchoosethesinglequoteasyourdelimitercharacter,thennovariableinterpolationisperformed
onthepattern.However,youstillneedtousethebackslashcharactertoescapeanyofthemeta
charactersdiscussedinthe"HowtoCreatePatterns"sectionlaterinthischapter.
http://affy.blogspot.my/p5be/ch10.htm
2/30
11/6/2015
Perl5byExample:RegularExpressions
Tip
Itendtoavoiddelimitersthatmightbeconfusedwithcharactersinthepattern.Forexample,usingthe
plussignasadelimiter(m+abc+)doesnothelpprogramreadability.Acasualreadermightthinkthat
youintendtoaddtwoexpressionsinsteadofmatchingthem.
Caution
The?hasaspecialmeaningwhenusedasamatchpatterndelimiter.Itworkslikethe/delimiter
exceptthatitmatchesonlyoncebetweencallstothereset()function.Thisfeaturemayberemoved
infutureversionsofPerl,soavoidusingit.
Thenextfewsectionslookatthematching,substitution,andtranslationoperatorsinmoredetail.
TheMatchingOperator(m//)
Thematchingoperator(m//)isusedtofindpatternsinstrings.Oneofitsmorecommonusesistolook
foraspecificstringinsideadatafile.Forinstance,youmightlookforallcustomerswhoselastname
is"Johnson"oryoumightneedalistofallnamesstartingwiththeletters.
Thematchingoperatoronlysearchesthe$_variable.Thismakesthematchstatementshorterbecause
youdon'tneedtospecifywheretosearch.Hereisaquickexample:
$_="AAAbbbAAA";
print"Foundbbb\n"ifm/bbb/;
Theprintstatementisexecutedonlyifthebbbcharactersequenceisfoundinthe$_variable.Inthis
particularcase,bbbwillbefound,sotheprogramwilldisplaythefollowing:
Foundbbb
Thematchingoperatorallowsyoutousevariableinterpolationinordertocreatethepattern.For
example:
$needToFind="bbb";
$_="AAAbbbAAA";
print"Foundbbb\n"ifm/$needToFind/;
UsingthematchingoperatorissocommonplacethatPerlallowsyoutoleaveoffthemfromthe
matchingoperatoraslongasslashesareusedasdelimiters:
$_="AAAbbbAAA";
print"Foundbbb\n"if/bbb/;
Usingthematchingoperatortofindastringinsideafileisveryeasybecausethedefaultsaredesigned
tofacilitatethisactivity.Forexample:
$target="M";
open(INPUT,"<findstr.dat");
while(<INPUT>){
if(/$target/){
print"Found$targetonline$.";
}
}
close(INPUT);
Note
The$.specialvariablekeepstrackoftherecordnumber.Everytimethediamondoperatorsreada
http://affy.blogspot.my/p5be/ch10.htm
3/30
11/6/2015
Perl5byExample:RegularExpressions
line,thisvariableisincremented.
ThisexamplereadseverylineinaninputsearchingfortheletterM.WhenanMisfound,theprint
statementisexecuted.Theprintstatementprintstheletterthatisfoundandthelinenumberitwas
foundon.
TheMatchingOptions
Thematchingoperatorhasseveraloptionsthatenhanceitsutility.Themostusefuloptionisprobably
thecapabilitytoignorecaseandtocreateanarrayofallmatchesinastring.Table10.2showsthe
optionsyoucanusewiththematchingoperator.
Table10.2OptionsfortheMatchingOperator
Option
Description
Thisoptionfindsalloccurrencesofthepatterninthestring.Alistofmatchesis
returnedoryoucaniterateoverthematchesusingaloopstatement.
Thisoptionignoresthecaseofcharactersinthestring.
Thisoptiontreatsthestringasmultiplelines.Perldoessomeoptimizationby
assumingthat$_containsasinglelineofinput.Ifyouknowthatitcontainsmultiple
newlinecharacters,usethisoptiontoturnofftheoptimization.
Thisoptioncompilesthepatternonlyonce.Youcanachievesomesmallperformance
gainswiththisoption.Itshouldbeusedwithvariableinterpolationonlywhenthe
valueofthevariablewillnotchangeduringthelifetimeoftheprogram.
Thisoptiontreatsthestringasasingleline.
Thisoptionletsyouuseextendedregularexpressions.Basically,thismeansthatPerl
willignorewhitespacethat'snotescapedwithabackslashorwithinacharacterclass.I
highlyrecommendthisoptionsoyoucanusespacestomakeyourregularexpressions
morereadable.Seethesection"Example:ExtensionSyntax"laterinthischapterfor
moreinformation.
Alloptionsarespecifiedafterthelastpatterndelimiter.Forinstance,ifyouwantthematchtoignore
thecaseofthecharactersinthestring,youcandothis:
$_="AAABBBAAA";
print"Foundbbb\n"ifm/bbb/i;
Thisprogramfindsamatcheventhoughthepatternuseslowercaseandthestringusesuppercase
becausethe/ioptionwasused,tellingPerltoignorethecase.
Theresultfromaglobalpatternmatchcanbeassignedtoanarrayvariableorusedinsidealoop.This
featurecomesinhandyafteryoulearnaboutmetacharactersinthesectioncalled"HowtoCreate
Patterns"laterinthischapter.
TheSubstitutionOperator(s///)
http://affy.blogspot.my/p5be/ch10.htm
4/30
11/6/2015
Perl5byExample:RegularExpressions
Thesubstitutionoperator(s///)isusedtochangestrings.Itrequirestwooperands,likethis:
s/a/z/;
Thisstatementchangesthefirstain$_intoaz.Nottoocomplicated,huh?Thingswon'tget
complicateduntilwestarttalkingaboutregularexpressionsinearnestinthesection"HowtoCreate
Patterns"laterinthechapter.
Youcanusevariableinterpolationwiththesubstitutionoperatorjustasyoucanwiththematching
operator.Forinstance:
$needToReplace="bbb";
$replacementText="1234567890";
$_="AAAbbbAAA";
$result=s/$needToReplace/$replacementText/;
Note
Youcanusevariableinterpolationinthereplacementpatternasshownhere,butnoneofthemeta
charactersdescribedlaterinthechaptercanbeusedinthereplacementpattern.
Thisprogramchangesthe$_variabletohold"AAA1234567890AAA"insteadofitsoriginalvalue,and
the$resultvariablewillbeequalto1thenumberofsubstitutionsmade.
Frequently,thesubstitutionoperatorisusedtoremovesubstrings.Forinstance,ifyouwanttoremove
the"bbb"sequenceofcharactersfromthe$_variable,youcoulddothis:
s/bbb//;
Byreplacingthematchedstringwithnothing,youhaveeffectivelydeletedit.
Ifbracketsofanytypeareusedasdelimitersforthesearchpattern,youneedtouseasecondsetof
bracketstoenclosethereplacementpattern.Forinstance:
$_="AAAbbbAAA";
$result=s{bbb}{1234567890};
TheSubstitutionOptions
Likethematchingoperator,thesubstitutionoperatorhasseveraloptions.Oneinterestingoptionisthe
capabilitytoevaluatethereplacementpatternasanexpressioninsteadofastring.Youcouldusethis
capabilitytofindallnumbersinafileandmultiplythembyagivenpercentage,forinstance.Oryou
couldrepeatmatchedstringsbyusingthestringrepetitionoperator.Table10.3showsallofthe
optionsyoucanusewiththesubstitutionoperator.
Table10.3OptionsfortheSubstitutionOperator
Option
Description
ThisoptionforcesPerltoevaluatethereplacementpatternasanexpression.
Thisoptionreplacesalloccurrencesofthepatterninthestring.
Thisoptionignoresthecaseofcharactersinthestring.
Thisoptiontreatsthestringasmultiplelines.Perldoessomeoptimizationby
assumingthat$_containsasinglelineofinput.Ifyouknowthatitcontainsmultiple
http://affy.blogspot.my/p5be/ch10.htm
5/30
11/6/2015
Perl5byExample:RegularExpressions
newlinecharacters,usethisoptiontoturnofftheoptimization.
o
Thisoptioncompilesthepatternonlyonce.Youcanachievesomesmallperformance
gainswiththisoption.Itshouldbeusedwithvariableinterpolationonlywhenthe
valueofthevariablewillnotchangeduringthelifetimeoftheprogram.
Thisoptiontreatsthestringasasingleline.
Thisoptionletsyouuseextendedregularexpressions.Basically,thismeansthatPerl
ignoreswhitespacethatisnotescapedwithabackslashorwithinacharacterclass.I
highlyrecommendthisoptionsoyoucanusespacestomakeyourregularexpressions
morereadable.Seethesection"Example:ExtensionSyntax"laterinthischapterfor
moreinformation.
The/eoptionchangestheinterpretationofthepatterndelimiters.Ifused,variableinterpolationis
activeevenifsinglequotesareused.Inaddition,ifbackquotesareusedasdelimiters,the
replacementpatternisexecutedasaDOSorUNIXcommand.Theoutputofthecommandisthen
usedasthereplacementtext.
TheTranslationOperator(tr///)
Thetranslationoperator(tr///)isusedtochangeindividualcharactersinthe$_variable.Itrequires
twooperands,likethis:
tr/a/z/;
Thisstatementtranslatesalloccurrencesofaintoz.Ifyouspecifymorethanonecharacterinthe
matchcharacterlist,youcantranslatemultiplecharactersatatime.Forinstance:
tr/ab/z/;
translatesallaandallbcharactersintothezcharacter.Ifthereplacementlistofcharactersisshorter
thanthetargetlistofcharacters,thelastcharacterinthereplacementlistisrepeatedasoftenas
needed.However,ifmorethanonereplacementcharacterisgivenforamatchedcharacter,onlythe
firstisused.Forinstance:
tr/WWW/ABC/;
resultsinallWcharactersbeingconvertedtoanAcharacter.Therestofthereplacementlistisignored.
Unlikethematchingandsubstitutionoperators,thetranslationoperatordoesn'tperformvariable
interpolation.
Note
ThetroperatorgetsitsnamefromtheUNIXtrutility.Ifyouarefamiliarwiththetrutility,thenyou
alreadyknowhowtousethetroperator.Z
TheUNIXsedutilityusesaytoindicatetranslations.TomakelearningPerleasierforsedusers,yis
supportedasasynonymfortr.
TheTranslationOptions
Thetranslationoperatorhasoptionsdifferentfromthematchingandsubstitutionoperators.Youcan
http://affy.blogspot.my/p5be/ch10.htm
6/30
11/6/2015
Perl5byExample:RegularExpressions
deletematchedcharacters,replacerepeatedcharacterswithasinglecharacter,andtranslateonly
charactersthatdon'tmatchthecharacterlist.Table10.4showsthetranslationoptions.
Table10.4OptionsfortheTranslationOperator
Option
Description
Thisoptioncomplementsthematchcharacterlist.Inotherwords,thetranslationis
doneforeverycharacterthatdoesnotmatchthecharacterlist.
Thisoptiondeletesanycharacterinthematchlistthatdoesnothaveacorresponding
characterinthereplacementlist.
Thisoptionreducesrepeatedinstancesofmatchedcharacterstoasingleinstanceof
thatcharacter.
Normally,ifthematchlistislongerthanthereplacementlist,thelastcharacterinthereplacementlist
isusedasthereplacementfortheextracharacters.However,whenthedoptionisused,thematched
charactersaresimplydeleted.
Ifthereplacementlistisempty,thennotranslationisdone.Theoperatorwillstillreturnthenumber
ofcharactersthatmatched,though.Thisisusefulwhenyouneedtoknowhowoftenagivenletter
appearsinastring.Thisfeaturealsocancompressrepeatedcharactersusingthesoption.
Tip
UNIXprogrammersmaybefamiliarwithusingthetrutilitytoconvertlowercasecharactersto
uppercasecharacters,orviceversa.Perlnowhasthelc()anduc()functionsthatcandothismuch
quicker.
TheBindingOperators(=~and!~)
Thesearch,modify,andtranslationoperationsworkonthe$_variablebydefault.Whatifthestringto
besearchedisinsomeothervariable?That'swherethebindingoperatorscomeintoplay.Theyletyou
bindtheregularexpressionoperatorstoavariableotherthan$_.Therearetwoformsofthebinding
operator:theregular=~anditscomplement!~.Thefollowingsmallprogramshowsthesyntaxofthe
=~operator:
$scalar="Theroothasmanyleaves";
$match=$scalar=~m/root/;
$substitution=$scalar=~s/root/tree/;
$translate=$scalar=~tr/h/H/;
print("\$match=$match\n");
print("\$substitution=$substitution\n");
print("\$translate=$translate\n");
print("\$scalar=$scalar\n");
Thisprogramdisplaysthefollowing:
$match=1
$substitution=1
$translate=2
$scalar=THetreeHasmanyleaves
Thisexampleusesallthreeoftheregularexpressionoperatorswiththeregularbindingoperator.Each
http://affy.blogspot.my/p5be/ch10.htm
7/30
11/6/2015
Perl5byExample:RegularExpressions
oftheregularexpressionoperatorswasboundtothe$scalarvariableinsteadof$_.Thisexamplealso
showsthereturnvaluesoftheregularexpressionoperators.Ifyoudon'tneedthereturnvalues,you
coulddothis:
$scalar="Theroothasmanyleaves";
print("Stringhasroot.\n")if$scalar=~m/root/;
$scalar=~s/root/tree/;
$scalar=~tr/h/H/;
print("\$scalar=$scalar\n");
Thisprogramdisplaysthefollowing:
Stringhasroot.
$scalar=THetreeHasmanyleaves
Theleftoperandofthebindingoperatoristhestringtobesearched,modified,ortransformedthe
rightoperandistheregularexpressionoperatortobeevaluated.
Thecomplementarybindingoperatorisvalidonlywhenusedwiththematchingregularexpression
operator.Ifyouuseitwiththesubstitutionortranslationoperator,yougetthefollowingmessageif
you'reusingthewcommandlineoptiontorunPerl:
Uselessuseofnotinvoidcontextattest.plline4.
Youcanseethatthe!~istheoppositeof=~byreplacingthe=~inthepreviousexample:
$scalar="Theroothasmanyleaves";
print("Stringhasroot.\n")if$scalar!~m/root/;
$scalar=~s/root/tree/;
$scalar=~tr/h/H/;
print("\$scalar=$scalar\n");
Thisprogramdisplaysthefollowing:
$scalar=THetreeHasmanyleaves
Thefirstprintlinedoesnotgetexecutedbecausethecomplementarybindingoperatorreturnsfalse.
HowtoCreatePatterns
Sofarinthischapter,you'vereadaboutthedifferentoperatorsusedwithregularexpressions,and
you'veseenhowtomatchsimplesequencesofcharacters.Nowwe'lllookatthewidearrayofmeta
charactersthatareusedtoharnessthefullpowerofregularexpressions.Metacharactersare
charactersthathaveanadditionalmeaningaboveandbeyondtheirliteralmeaning.Forexample,the
periodcharactercanhavetwomeaningsinapattern.First,itcanbeusedtomatchaperiodcharacter
inthesearchedstringthisisitsliteralmeaning.Andsecond,itcanbeusedtomatchanycharacterin
thesearchedstringexceptforthenewlinecharacterthisisitsmetameaning.
Whencreatingpatterns,themetameaningwillalwaysbethedefault.Ifyoureallyintendtomatchthe
literalcharacter,youneedtoprefixthemetacharacterwithabackslash.Youmightrecallthatthe
backslashisusedtocreateanescapesequence.
Patternscanhavemanydifferentcomponents.Thesecomponentsallcombinetoprovideyouwiththe
powertomatchanytypeofstring.Thefollowinglistofcomponentswillgiveyouagoodideaofthe
varietyofwaysthatpatternscanbecreated.Thesection"PatternExamples"laterinthischapter
showsmanyexamplesoftheserulesinaction.
VariableInterpolation:Anyvariableisinterpolated,andtheessentiallynewpatternisthen
http://affy.blogspot.my/p5be/ch10.htm
8/30
11/6/2015
Perl5byExample:RegularExpressions
evaluatedasaregularexpression.Rememberthatonlyonelevelofinterpolationisdone.This
meansthatifthevalueofthevariableincludes,forexample,$scalarasastringvalue,then
$scalarwillnotbeinterpolated.Inaddition,backquotesdonotinterpolatewithindouble
quotes,andsinglequotesdonotstopinterpolationofvariableswhenusedwithindouble
quotes.
SelfMatchingCharacters:Anycharacterwillmatchitselfunlessitisametacharacterorone
of$,@,%,&.ThemetacharactersarelistedinTable10.5,andtheothercharactersareusedto
beginvariablenamesandfunctioncalls.YoucanusethebackslashcharactertoforcePerlto
matchtheliteralmeaningofanycharacter.Forexample,m/a/;willreturntrueiftheletterais
inthe$_variable.Andm/\$/;willreturntrueifthecharacter$isinthe$_variable.
Table10.5RegularExpressionMetaCharacters,MetaBrackets,andMetaSequences
Meta
Character
Description
Thismetacharacterthecaretwillmatchthebeginningofastringorifthe
/moptionisused,matchesthebeginningofaline.Itisoneoftwopattern
anchorstheotheranchoristhe$.
Thismetacharacterwillmatchanycharacterexceptforthenewlineunless
the/soptionisspecified.Ifthe/soptionisspecified,thenthenewlinewill
alsobematched.
Thismetacharacterwillmatchtheendofastringorifthe/moptionisused,
matchestheendofaline.Itisoneoftwopatternanchorstheotheranchor
isthe^.
Thismetacharactercalledalternationletsyouspecifytwovaluesthatcan
causethematchtosucceed.Forinstance,m/a|b/meansthatthe$_variable
mustcontainthe"a"or"b"characterforthematchtosucceed.
Thismetacharacterindicatesthatthe"thing"immediatelytotheleftshould
bematched0ormoretimesinordertobeevaluatedastrue.
Thismetacharacterindicatesthatthe"thing"immediatelytotheleftshould
bematched1ormoretimesinordertobeevaluatedastrue.
Thismetacharacterindicatesthatthe"thing"immediatelytotheleftshould
bematched0or1timesinordertobeevaluatedastrue.Whenusedin
conjunctionwiththe+,_,?,or{n,m}metacharactersandbrackets,itmeans
thattheregularexpressionshouldbenongreedyandmatchthesmallest
possiblestring.
Meta
Brackets
()
Description
Theparenthesesletyouaffecttheorderofpatternevaluationandactasaform
ofpatternmemory.Seethesection"PatternMemory"laterinthischapterfor
http://affy.blogspot.my/p5be/ch10.htm
9/30
11/6/2015
Perl5byExample:RegularExpressions
moreinformation.
(?...)
Ifaquestionmarkimmediatelyfollowstheleftparentheses,itindicatesthat
anextendedmodecomponentisbeingspecified.Seethesection"Example:
ExtensionSyntax"laterinthischapterformoreinformation.
{n,m}
Thecurlybracesletspecifyhowmanytimesthe"thing"immediatelytothe
leftshouldbematched.{n}meansthatitshouldbematchedexactlyntimes.
{n,}meansitmustbematchedatleastntimes.{n,m}meansthatitmustbe
matchedatleastntimesandnotmorethanmtimes.
[]
Thesquarebracketsletyoucreateacharacterclass.Forinstance,m/[abc]/
willevaluatetotrueifanyof"a","b",or"c"iscontainedin$_.Thesquare
bracketsareamorereadablealternativetothealternationmetacharacter.
Meta
Sequences
Description
Thismetacharacter"escapes"thefollowingcharacter.Thismeansthatany
specialmeaningnormallyattachedtothatcharacterisignored.Forinstance,
ifyouneedtoincludeadollarsigninapattern,youmustuse\$toavoid
Perl'svariableinterpolation.Use\\tospecifythebackslashcharacterin
yourpattern.
\nnn
AnyOctalbyte.Usezeropaddingforvaluesfrom\000to\077inclusively.
Forlargervaluessimplyusethethreedigitnumber(like\100or\323).
\a
Alarm.
\A
Thismetasequencerepresentsthebeginningofthestring.Itsmeaningisnot
affectedbythe/moption.
\b
Thismetasequencerepresentsthebackspacecharacterinsideacharacter
classotherwise,itrepresentsawordboundary.Awordboundaryisthespot
betweenword(\w)andnonword(\W)characters.Perlthinksthatthe\Wmeta
sequencematchestheimaginarycharactersofftheendsofthestring.
\B
Matchanonwordboundary.
\cn
Anycontrolcharacter.
\d
Matchasingledigitcharacter.
\D
Matchasinglenondigitcharacter.
\e
Escape.
\E
Terminatethe\Lor\Usequence.
http://affy.blogspot.my/p5be/ch10.htm
10/30
11/6/2015
Perl5byExample:RegularExpressions
\f
FormFeed.
\G
Matchonlywherethepreviousm//gleftoff.
\l
Changethenextcharactertolowercase.
\L
Changethefollowingcharacterstolowercaseuntila\Esequenceis
encountered.
\n
Newline.
\Q
QuoteRegularExpressionmetacharactersliterallyuntilthe\Esequenceis
encountered.
\r
CarriageReturn.
\s
Matchasinglewhitespacecharacter.
\S
Matchasinglenonwhitespacecharacter.
\t
Tab.
\u
Changethenextcharactertouppercase.
\U
Changethefollowingcharacterstouppercaseuntila\Esequenceis
encountered.
\v
VerticalTab.
\w
Matchasinglewordcharacter.Wordcharactersarethealphanumericand
underscorecharacters.
\W
Matchasinglenonwordcharacter.
\xnn
AnyHexadecimalbyte.
\Z
Thismetasequencerepresentstheendofthestring.Itsmeaningisnot
affectedbythe/moption.
\$
DollarSign.
\@
Ampersand.
\%
PercentSign.
ErrataNote
The\%isnotavalidescapesequenceforPerl.Itwasincludederroneously.
http://affy.blogspot.my/p5be/ch10.htm
11/30
11/6/2015
Perl5byExample:RegularExpressions
Pleaseignorethisentry.Ifyouneedtousethe%characterindoublequoted
strings,goaheadanduseit.Laterinthebook,you'llreadabouttheprintf()
function.Ifyouwanttoactuallyusethe%characterintheprintf()format
string,usethe%%sequence.RandalSchwartzwaskindenoughtoidentify
thiserror.
CharacterSequences:Asequenceofcharacterswillmatchtheidenticalsequenceinthe
searchedstring.Thecharactersneedtobeinthesameorderinboththepatternandthesearched
stringforthematchtobetrue.Forexample,m/abc/;willmatch"abc"butnot"cab"or"bca".If
anycharacterinthesequenceisametacharacter,youneedtousethebackslashtomatchits
literalvalue.
Alternation:Thealternationmetacharacter(|)willletyoumatchmorethanonepossible
string.Forexample,m/a|b/;willmatchifeitherthe"a"characterorthe"b"characterisinthe
searchedstring.Youcanusesequencesofmorethanonecharacterwithalternation.For
example,m/dog|cat/;willmatchifeitherofthestrings"dog"or"cat"isinthesearchedstring.
Tip
Someprogrammersliketoenclosethealternationsequenceinsideparenthesestohelpindicate
wherethesequencebeginsandends.
m/(dog|cat)/;
However,thiswillaffectsomethingcalledpatternmemory,whichyou'llbelearningaboutin
thesection"Example:PatternMemory"laterinthechapter.
CharacterClasses:Thesquarebracketsareusedtocreatecharacterclasses.Acharacterclass
isusedtomatchaspecifictypeofcharacter.Forexample,youcanmatchanydecimaldigit
usingm/[0123456789]/.Thiswillmatchasinglecharacterintherangeofzerotonine.Youcan
findmoreinformationaboutcharacterclassesinthesectioncalled"Example:Character
Classes"laterinthischapter.
ErrataNote
Theprintedversionofthisbooksays:m/0123456789/;.Thesquarebracketsweremissingand
thesemicolonisextraneoussincethisisanexampleofanexpression,notastatement.Randal
Schwartzwaskindenoughtopointoutthisproblem.
SymbolicCharacterClasses:Thereareseveralcharacterclassesthatareusedsofrequently
thattheyhaveasymbolicrepresentation.Theperiodmetacharacterstandsforaspecial
characterclassthatmatchesallcharactersexceptforthenewline.Therestare\d,\D,\s,\S,\w,
and\W.ThesearementionedinTable10.5earlierandarediscussedinthesection"Example:
CharacterClasses"laterinthischapter.
Anchors:Thecaret(^)andthedollarsignmetacharactersareusedtoanchorapatterntothe
beginningandtheendofthesearchedstring.Thecaretisalwaysthefirstcharacterinthe
patternwhenusedasananchor.Forexample,m/^one/;willonlymatchifthesearchedstring
startswithsequenceofcharacters,one.Thedollarsignisalwaysthelastcharacterinthepattern
whenusedasananchor.Forexample,m/(last|end)$/;willmatchonlyifthesearchedstring
endswitheitherthecharactersequencelastorthecharactersequenceend.The\Aand\Zmeta
sequencesarealsousedaspatternanchorsforthebeginningandendofstrings.
ErrataNote
Theprintedversionofthisbookstates"Thecaretisalwaysthefirstcharacterinthepattern
whenusedasananchor".However,thisisnotstrictlytruewhenthealternationmetacharacter
isused.Forexample,/Jack$|^John/willmatchwhen"Jack"isattheendofastringorwhen
"John"isatthebeginningofastring.RandalSchwartzwaskindenoughtomentionthatthis
http://affy.blogspot.my/p5be/ch10.htm
12/30
11/6/2015
Perl5byExample:RegularExpressions
conceptneedsclarification.
Quantifiers:Thereareseveralmetacharactersthataredevotedtocontrollinghowmany
charactersarematched.Forexample,m/a{5}/;meansthatfiveacharactersmustbefound
beforeatrueresultcanbereturned.The*,+,and?metacharactersandthecurlybracesareall
usedasquantifiers.Seethesection"Example:Quantifiers"laterinthischapterformore
information.
PatternMemory:Parenthesesareusedtostorematchedvaluesintobuffersforlaterrecall.I
liketothinkofthisasaformofpatternmemory.Someprogrammerscallthembackreferences.
Afteryouusem/(fish|fowl)/;tomatchastringandamatchisfound,thevariable$1willhold
eitherfishorfowldependingonwhichsequencewasmatched.Seethesection"Example:
PatternMemory"laterinthischapterformoreinformation.
WordBoundaries:The\bmetasequencewillmatchthespotbetweenaspaceandthefirst
characterofawordorbetweenthelastcharacterofawordandthespace.The\bwillmatchat
thebeginningorendofastringiftherearenoleadingortrailingspaces.Forexample,
m/\bfoo/;willmatchfooevenwithoutspacessurroundingtheword.Itwillalsomatch$foo
becausethedollarsignisnotconsideredawordcharacter.Thestatementm/foo\b/;willmatch
foobutnotfoobar,andthestatementm/\bwiz/;willmatchwizardbutnotgeewiz.Seethe
section"Example:CharacterClasses"laterinthischapterformoreinformationaboutword
boundaries.
The\Bmetasequencewillmatcheverywhereexceptatawordboundary.
QuotingMetaCharacters:Youcanmatchmetacharacterliterallybyenclosingthemina
\Q..\Esequence.Thiswillletyouavoidusingthebackslashcharactertoescapeallmeta
characters,andyourcodewillbeeasiertoread.
ExtendedSyntax:The(?...)sequenceletsyouuseanextendedversionoftheregular
expressionsyntax.Thedifferentoptionsarediscussedinthesection"Example:Extension
Syntax"laterinthischapter.
Combinations:Anyoftheprecedingcomponentscanbecombinedwithanyothertocreate
simpleorcomplexpattern.
Thepowerofpatternsisthatyoudon'talwaysknowinadvancethevalueofthestringthatyouwillbe
searching.Ifyouneedtomatchthefirstwordinastringthatwasreadinfromafile,youprobably
havenoideahowlongitmightbetherefore,youneedtobuildapattern.Youmightstartwiththe\w
symboliccharacterclass,whichwillmatchanysinglealphanumericorunderscorecharacter.So,
assumingthatthestringisinthe$_variable,youcanmatchaonecharacterwordlikethis:
m/\w/;
Ifyouneedtomatchbothaonecharacterwordandatwocharacterword,youcandothis:
m/\w|\w\w/;
Thispatternsaystomatchasinglewordcharacterortwoconsecutivewordcharacters.Youcould
continuetoaddalternationcomponentstomatchthedifferentlengthsofwordsthatyoumightexpect
tosee,butthereisabetterway.
Youcanusethe+quantifiertosaythatthematchshouldsucceedonlyifthecomponentismatched
oneormoretimes.Itisusedthisway:
m/\w+/;
http://affy.blogspot.my/p5be/ch10.htm
13/30
11/6/2015
Perl5byExample:RegularExpressions
Ifthevalueof$_was"AAABBB",thenm/\w+/;wouldmatchthe"AAA"inthestring.If$_wasblank,
fullofwhitespace,orfullofothernonwordcharacters,anundefinedvaluewouldbereturned.
Theprecedingpatternwillletyoudetermineif$_containsawordbutdoesnotletyouknowwhatthe
wordis.Inordertoaccomplishthat,youneedtoenclosethematchingcomponentsinsideparentheses.
Forexample:
m/(\w+)/;
Bydoingthis,youforcePerltostorethematchedstringintothe$1variable.The$1variablecanbe
consideredaspatternmemory.
Thisintroductiontopatterncomponentsdescribesmostofthedetailsyouneedtoknowinorderto
createyourownpatternsorregularexpressions.However,someofthecomponentsdeserveabitmore
study.Thenextfewsectionslookatcharacterclasses,quantifiers,patternmemory,pattern
precedence,andtheextensionsyntax.Thentherestofthechapterisdevotedtoshowingspecific
examplesofwhentousethedifferentcomponents.
Example:CharacterClasses
Acharacterclassdefinesatypeofcharacter.Thecharacterclass[0123456789]definestheclassof
decimaldigits,and[09af]definestheclassofhexadecimaldigits.Noticethatyoucanuseadashto
definearangeofconsecutivecharacters.Characterclassesletyoumatchanyofarangeofcharacters
youdon'tknowinadvancewhichcharacterwillbematched.Thiscapabilitytomatchnonspecific
charactersiswhatmetacharactersareallabout.
Youcanusevariableinterpolationinsidethecharacterclass,butyoumustbecarefulwhendoingso.
Forexample,
$_="AAABBBCCC";
$charList="ADE";
print"matched"ifm/[$charList]/;
willdisplay
matched
Thisisbecausethevariableinterpolationresultsinacharacterclassof[ADE].Ifyouusethevariable
asonehalfofacharacterrange,youneedtoensurethatyoudon'tmixnumbersanddigits.For
example,
$_="AAABBBCCC";
$charList="ADE";
print"matched"ifm/[$charList9]/;
willresultinthefollowingerrormessagewhenexecuted:
/[ADE9]/:invalid[]rangeinregexpattest.plline4.
Attimes,it'snecessarytomatchonanycharacterexceptforagivencharacterlist.Thisisdoneby
complementingthecharacterclasswiththecaret.Forexample,
$_="AAABBBCCC";
print"matched"ifm/[^ABC]/;
willdisplaynothing.ThismatchreturnstrueonlyifacharacterbesidesA,B,orCisinthesearched
string.IfyoucomplementalistwithjusttheletterA,
http://affy.blogspot.my/p5be/ch10.htm
14/30
11/6/2015
Perl5byExample:RegularExpressions
$_="AAABBBCCC";
print"matched"ifm/[^A]/;
thenthestring"matched"willbedisplayedbecauseBandCarepartofthestringinotherwords,a
characterbesidestheletterA.
Perlhasshortcutsforsomecharacterclassesthatarefrequentlyused.HereisalistofwhatIcall
symboliccharacterclasses:
\wThissymbolmatchanyalphanumericcharacterortheunderscorecharacter.Itisequivalent
tothecharacterclass[azAZ09_].
\WThissymbolmatcheseverycharacterthatthe\wsymboldoesnot.Inotherwords,itisthe
complementof\w.Itisequivalentto[^azAZ09_].
\sThissymbolmatchesanyspace,tab,ornewlinecharacter.Itisequivalentto[\t\n].
\SThissymbolmatchesanynonwhitespacecharacter.Itisequivalentto[^\t\n].
\dThissymbolmatchanydigit.Itisequivalentto[09].
\DThissymbolmatchesanynondigitcharacter.Itisequivalentto[^09].
Youcanusethesesymbolsinsideothercharacterclassesbutnotasendpointsofarange.Forexample,
youcandothefollowing:
$_="\tAAA";
print"matched"ifm/[\d\s]/;
whichwilldisplay
matched
becausethevalueof$_includesthetabcharacter.
Tip
Metacharactersthatappearinsidethesquarebracketsthatdefineacharacterclassareusedintheir
literalsense.Theylosetheirmetameaning.Thismaybealittleconfusingatfirst.Infact,Ihavea
tendencytoforgetthiswhenevaluatingpatterns.
Note
Ithinkthatmostoftheconfusionregardingregularexpressionsliesinthefactthateachcharacterofa
patternmighthaveseveralpossiblemeanings.Thecaretcouldbeananchor,itcouldbeacaret,orit
couldbeusedtocomplementacharacterclass.Therefore,itisvitalthatyoudecidewhichcontextany
givenpatterncharacterorsymbolisinbeforeassigningameaningtoit.
Example:Quantifiers
Perlprovidesseveraldifferentquantifiersthatletyouspecifyhowmanytimesagivencomponent
mustbepresentbeforethematchistrue.Theyareusedwhenyoudon'tknowinadvancehowmany
charactersneedtobematched.Table10.6liststhedifferentquantifiersthatcanbeused.
Table10.6TheSixTypesofQuantifiers
Quantifier
Description
http://affy.blogspot.my/p5be/ch10.htm
15/30
11/6/2015
Perl5byExample:RegularExpressions
Thecomponentmustbepresentzeroormoretimes.
Thecomponentmustbepresentoneormoretimes.
Thecomponentmustbepresentzerooronetimes.
{n}
Thecomponentmustbepresentntimes.
{n,}
Thecomponentmustbepresentatleastntimes.
{n,m}
Thecomponentmustbepresentatleastntimesandnomorethanmtimes.
Ifyouneedtomatchawordwhoselengthisunknown,youneedtousethe+quantifier.Youcan'tuse
an*becauseazerolengthwordmakesnosense.So,thematchstatementmightlooklikethis:
m/^\w+/;
Thispatternwillmatch"QQQ"and"AAAAA"butnot""or"BBB".Inordertoaccountfortheleading
whitespace,whichmayornotbeatthebeginningofastring,youneedtousetheasterisk(*)
quantifierinconjunctionwiththe\ssymboliccharacterclassinthefollowingway:
m/\s*\w+/;
Tip
Becarefulwhenusingthe*quantifierbecauseitcanmatchanemptystring,whichmightnotbeyour
intention.Thepattern/b*/willmatchanystringevenonewithoutanybcharacters.
ErrataNote
Theprintedversionofthisbookhasthefirstmatchstatementas
m/\w+/;
,noticethatpatternanchorwasleftout.
Attimes,youmayneedtomatchanexactnumberofcomponents.Thefollowingmatchstatementwill
betrueonlyiffivewordsarepresentinthe$_variable:
$_="AAABACADAE";
m/^(\w+\W+){5}$/;
Inthisexample,wearematchingatleastonewordcharacterfollowedbyzeroormorenonword
characters.NoticethatPerlconsiderstheendofastringasanonwordcharacter.The{5}quantifieris
usedtoensurethatthatcombinationofcomponentsispresentfivetimes.
ErrataNote
Theprintedversionofthebookusedthepatternm/(\w+\s*){5}/;inordertomatchthefivewords.
Thisisincorrectsincethepattern\w+\s*matchesasinglecharacter(rememberthat*matcheszeroor
moreinstancesofacharacter).Thereforem/(\w+\s*){5}/;matches"AAAA"aswellas"AAAAA".
The*and+quantifiersaregreedy.Theymatchasmanycharactersaspossible.Thismaynotalways
bethebehaviorthatyouneed.Youcancreatenongreedycomponentsbyfollowingthequantifier
witha?.
Usethefollowingfilespecificationinordertolookatthe*and+quantifiersmoreclosely:
http://affy.blogspot.my/p5be/ch10.htm
16/30
11/6/2015
Perl5byExample:RegularExpressions
$_='/user/Jackie/temp/names.dat';
Theregularexpression.*willmatchtheentirefilespecification.Thiscanbeseeninthefollowing
smallprogram:
$_='/user/Jackie/temp/names.dat';
m/.*/;
print$&;
Thisprogramdisplays
/user/Jackie/temp/names.dat
Youcanseethatthe*quantifierisgreedy.Itmatchedthewholestring.Ifyouaddthe?modifierto
makethe.*componentnongreedy,whatdoyouthinktheprogramwoulddisplay?
$_='/user/Jackie/temp/names.dat';
m/.*?/;
print$&;
Thisprogramdisplaysnothingbecausetheleastamountofcharactersthatthe*matchesiszero.Ifwe
changethe*toa+,thentheprogramwilldisplay
/
Next,let'slookattheconceptofpatternmemory,whichletsyoukeepbitsofmatchedstringaround
afterthematchiscomplete.
Example:PatternMemory
Matchingarbitrarynumbersofcharactersisfine,butwithoutthecapabilitytofindoutwhatwas
matched,patternswouldbenotveryuseful.Perlletsyouenclosepatterncomponentsinside
parenthesesinordertostorethestringthatmatchedthecomponentsintopatternmemory.Youmight
alsohearpatternmemoryreferredtoaspatternbuffers.Thismemorypersistsafterthematch
statementisfinishedexecutingsothatyoucanassignthematchedvaluestoothervariables.
Yousawasimpleexampleofthisearlierrightafterthecomponentdescriptions.Thatexamplelooked
forthefirstwordinastringandstoreditintothefirstbuffer,$1.Thefollowingsmallprogram
$_="AAABBBCCC";
m/(\w+)/;
print("$1\n");
willdisplay
AAA
Youcanuseasmanybuffersasyouneed.Eachtimeyouaddasetofparentheses,anotherbufferis
used.Thepatternmatchedbythefirstsetisplacedinto$1.Thepatternmatchedbythesecondsetis
placedinto$2.Andsoon.
Ifyouwanttofindallthewordsinthestring,youneedtousethe/gmatchoption.Inordertofindall
thewords,youcanusealoopstatementthatloopsuntilthematchoperatorreturnsfalse.
$_="AAABBBCCC";
while(m/(\w+)/g){
print("$1\n");
}
http://affy.blogspot.my/p5be/ch10.htm
17/30
11/6/2015
Perl5byExample:RegularExpressions
Theprogramwilldisplay
AAA
BBB
CCC
Ifloopingthroughthematchesisnottherightapproachforyourneeds,perhapsyouneedtocreatean
arrayconsistingofthematches.
$_="AAABBBCCC";
@matches=m/(\w+)/g;
print("@matches\n");
Theprogramwilldisplay
AAABBBCCC
Perlalsohasafewspecialvariablestohelpyouknowwhatmatchedandwhatdidnot.These
variableswilloccasionallysaveyoufromhavingtoaddparenthesestofindinformation.
$+Thisvariableisassignedthevaluethatthelastbracketmatchmatched.
$&Thisvariableisassignedthevalueoftheentirematchedstring.Ifthematchisnot
successful,then$&retainsitsvaluefromthelastsuccessfulmatch.
$`Thisvariableisassignedeverythinginthesearchedstringthatisbeforethematchedstring.
$'Thisvariableisassignedeverythinginthesearchstringthatisafterthematchedstring.
Tip
Ifyouneedtosavethevalueofthematchedstringsstoredinthepatternmemory,makesuretoassign
themtoothervariables.Patternmemoryislocaltotheenclosingblockandlastsonlyuntilanother
matchisdone.
Example:PatternPrecedence
Patterncomponentshaveanorderofprecedencejustasoperatorsdo.Ifyouseethefollowingpattern:
m/a|b+/
it'shardtotellifthepatternshouldbe
m/(a|b)+/#matchanysequenceof"a"and"b"characters
#inanyorder.
or
m/a|(b+)/#matcheitherthe"a"characterorthe"b"character
#repeatedoneormoretimes.
TheorderofprecedenceshowninTable10.7isdesignedtosolveproblemslikethis.Bylookingat
thetable,youcanseethatquantifiershaveahigherprecedencethanalternation.Therefore,thesecond
interpretationiscorrect.
Table10.7ThePatternComponentOrderof
Precedence
PrecedenceLevel
http://affy.blogspot.my/p5be/ch10.htm
Component
18/30
11/6/2015
Perl5byExample:RegularExpressions
Parentheses
Quantifiers
SequencesandAnchors
Alternation
Tip
Youcanuseparenthesestoaffecttheorderthatcomponentsareevaluatedbecausetheyhavethe
highestprecedence.However,unlessyouusetheextendedsyntax,youwillbeaffectingthepattern
memory.
Example:ExtensionSyntax
Theregularexpressionextensionsareawaytosignificantlyaddtothepowerofpatternswithout
addingalotofmetacharacterstotheproliferationthatalreadyexists.Byusingthebasic(?...)
notation,theregularexpressioncapabilitiescanbegreatlyextended.
Atthistime,Perlrecognizesfiveextensions.Thesevarywidelyinfunctionalityfromadding
commentstosettingoptions.Table10.8liststheextensionsandgivesashortdescriptionofeach.
Table10.8FiveExtensionComponents
Extension
Description
(?#
TEXT)
Thisextensionletsyouaddcommentstoyourregularexpression.TheTEXTvalue
isignored.
(?:...)
Thisextensionletsyouaddparenthesestoyourregularexpressionwithoutcausing
apatternmemorypositiontobeused.
(?=...)
Thisextensionletsyoumatchvalueswithoutincludingtheminthe$&variable.
(?!...)
Thisextensionletsyouspecifywhatshouldnotfollowyourpattern.Forinstance,
/blue(?!bird)/meansthat"bluebox"and"bluesy"willbematchedbutnot
"bluebird".
(?sxi)
Thisextensionletsyouspecifyanembeddedoptioninthepatternratherthan
addingitafterthelastdelimiter.Thisisusefulifyouarestoringpatternsin
variablesandusingvariableinterpolationtodothematching.
Byfarthemostusefulfeatureofextendedmode,inmyopinion,istheabilitytoaddcomments
directlyinsideyourpatterns.Forexample,wouldyouratheraseeapatternthatlookslikethis:
#Matchastringwithtwowords.$1willbethe
#firstword.$2willbethesecondword.
m/^\s*(\w+)\W+(\w+)\s*$/;
oronethatlookslikethis:
http://affy.blogspot.my/p5be/ch10.htm
19/30
11/6/2015
Perl5byExample:RegularExpressions
m/
(?#Thispatternwillmatchanystringwithtwo)
(?#andonlytwowordsinit.Thematchedwords)
(?#willbeavailablein$1and$2ifthematch)
(?#issuccessful.)
^(?#Anchorthismatchtothebeginning)
(?#ofthestring)
\s*(?#skipoveranywhitespacecharacters)
(?#usethe*becausetheremaybenone)
(\w+)(?#Matchthefirstword,weknowit's)
(?#thefirstwordbecauseoftheanchor)
(?#above.Placethematchedwordinto)
(?#patternmemory.)
\W+(?#Matchatleastonenonword)
(?#character,theremaybemorethanone)
(\w+)(?#Matchanotherword,putintopattern)
(?#memoryalso.)
\s*(?#skipoveranywhitespacecharacters)
(?#usethe*becausetheremaybenone)
$(?#Anchorthismatchtotheendofthe)
(?#string.Becauseboth^and$anchors)
(?#arepresent,theentirestringwill)
(?#needtomatchthepattern.A)
(?#substringthatfitsthepatternwill)
(?#notmatch.)
/x;
Ofcourse,thecommentedpatternismuchlonger,buttheytakethesameamountoftimetoexecute.
Inaddition,itwillbemucheasiertomaintainthecommentedpatternbecauseeachcomponentis
explained.Whenyouknowwhateachcomponentisdoinginrelationtotherestofthepattern,it
becomeseasytomodifyitsbehaviorwhentheneedarises.
Extensionsalsoletyouchangetheorderofevaluationwithoutaffectingpatternmemory.For
example,
m/(?:a|b)+/;
matchestheaorbcharactersrepeatedoneormoretimesinanyorder.Thepatternmemorywillnotbe
affected.
Attimes,youmightliketoincludeapatterncomponentinyourpatternwithoutincludingitinthe$&
variablethatholdsthematchedstring.Thetechnicaltermforthisisazerowidthpositivelookahead
assertion.Youcanusethistoensurethatthestringfollowingthematchedcomponentiscorrect
withoutaffectingthematchedvalue.Forexample,ifyouhavesomedatathatlookslikethis:
DavidVeterinarian56
JackieOrthopedist34
KarenVeterinarian28
andyouwanttofindallveterinariansandstorethevalueofthefirstcolumn,youcanusealookahead
assertion.Thiswilldobothtasksinonestep.Forexample:
while(<>){
push(@array,$&)ifm/^\w+(?=\s+Vet)/;
}
http://affy.blogspot.my/p5be/ch10.htm
20/30
11/6/2015
Perl5byExample:RegularExpressions
print("@array\n");
Thisprogramwilldisplay:
DavidKaren
Let'slookatthepatternwithcommentsaddedusingtheextendedmode.Inthiscase,itdoesn'tmake
sensetoaddcommentsdirectlytothepatternbecausethepatternispartoftheifstatementmodifier.
Addingcommentsinthatlocationwouldmakethecommentshardtoformat.Solet'suseadifferent
tactic.
$pattern='^\w+(?#Matchthefirstwordinthestring)
(?=\s+(?#Usealookaheadassertiontomatch)
(?#oneormorewhitespacecharacters)
Vet)(?#Inadditiontothewhitespace,make)
(?#surethatthenextcolumnstarts)
(?#withthecharactersequence"Vet")
';
while(<>){
push(@array,$&)ifm/$pattern/x;
}
print("@array\n");
Hereweusedavariabletoholdthepatternandthenusedvariableinterpolationinthepatternwiththe
matchoperator.Youmightwanttopickamoredescriptivevariablenamethan$pattern,however.
Tip
AlthoughthePerldocumentationdoesnotmentionit,Ibelieveyouhaveonlyonelookahead
assertionperpattern,anditmustbethelastpatterncomponent.
Thelastextensionthatwe'lldiscussisthezerowidthnegativeassertion.Thistypeofcomponentis
usedtospecifyvaluesthatshouldn'tfollowthematchedstring.Forexample,usingthesamedataasin
thepreviousexample,youcanlookforeveryonewhoisnotaveterinarian.Yourfirstinclination
mightbetosimplyreplacethe(?=...)withthe(?!...)inthepreviousexample.
while(<>){
push(@array,$&)ifm/^\w+(?!\s+Vet)/;
}
print("@array\n");
Unfortunately,thisprogramdisplays
DaviJackieKare
whichisnotwhatyouneed.TheproblemisthatPerlislookingatthelastcharacterofthewordtosee
ifitmatchestheVetcharactersequence.Inordertocorrectlymatchthefirstword,youneedto
explicitlytellPerlthatthefirstwordendsatawordboundary,likethis:
while(<>){
push(@array,$&)ifm/^\w+\b(?!\s+Vet)/;
}
print("@array\n");
Thisprogramdisplays
http://affy.blogspot.my/p5be/ch10.htm
21/30
11/6/2015
Perl5byExample:RegularExpressions
Jackie
whichiscorrect.
Tip
Therearemanywaysofmatchinganyvalue.Ifthefirstmethodyoutrydoesn'twork,trybreakingthe
valueintosmallercomponentsandmatcheachboundary.Ifallelsefails,youcanalwaysaskforhelp
onthecomp.lang.perl.miscnewsgroup.
PatternExamples
Inordertodemonstratemanydifferentpatterns,Iwilldepartfromthestandardexampleformatinthis
section.Instead,Iwillexplainamatchingsituationinitalicizedtextandthenapossibleresolutionwill
immediatelyfollow.Aftertheresolution,I'lladdsomecommentstoexplainhowthematchisdone.In
alloftheseexamples,thestringtosearchwillbeinthe$_variable.
Example:UsingtheMatchOperator
IfyouneedtofindrepeatedcharactersinastringliketheAAin"ABCAAABC",thendothis:
m/(.)\1/;
Thispatternusespatternmemorytostoreasinglecharacter.Thenabackreference(\1)isused
torepeatthefirstcharacter.Thebackreferenceisusedtoreferencethepatternmemorywhile
stillinsidethepattern.Anywhereelseintheprogram,usethe$1variable.Afterthisstatement,
$1willholdtherepeatedcharacter.Thispatternwillmatchtwoofanynonnewlinecharacter.
Ifyouneedtofindthefirstwordinastring,thendothis:
m/^\s*(\w+)/;
Afterthisstatement,$1willholdthefirstwordinthestring.Anywhitespaceatthebeginningof
thestringwillbeskippedbythe\s*metacharactersequence.Thenthe\w+metacharacter
sequencewillmatchthenextword.Notethatthe*whichmatcheszeroormoreisusedto
matchthewhitespacebecausetheremaynotbeany.The+whichmatchesoneormoreis
usedfortheword.
Ifyouneedtofindthelastwordinastring,thendothis:
m/
(\w+)(?#Matchaword,storeitsvalueintopatternmemory)
[.!?]?(?#Somestringsmightholdasentence.Ifso,this)
(?#componentwillmatchzerooronepunctuation)
(?#characters)
\s*(?#Matchtrailingwhitespaceusingthe*becausethere)
(?#mightnotbeany)
$(?#Anchorthematchtotheendofthestring)
/x;
Afterthisstatement,$1willholdthelastwordinthestring.Youneedtoexpandthecharacter
class,[.!?],byaddingmorepunctuation.
Ifyouneedtoknowthatthereareonlytwowordsinastring,youcandothis:
http://affy.blogspot.my/p5be/ch10.htm
22/30
11/6/2015
Perl5byExample:RegularExpressions
m/^(\w+)\W+(\w+)$/x;
Afterthisstatement,$1willholdthefirstwordand$2willholdthesecondword,assumingthat
thepatternmatches.Thepatternstartswithacaretandendswithadollarsign,whichmeans
thattheentirestringmustmatchthepattern.The\w+metacharactersequencematchesone
word.The\W+metacharactersequencematchesthewhitespacebetweenwords.Youcantest
foradditionalwordsbyaddingone\W+(\w+)metacharactersequenceforeachadditionalword
tomatch.
Ifyouneedtoknowthatthereareonlytwowordsinastringwhileignoringleadingortrailing
spaces,youcandothis:
m/^\s*(\w+)\W+(\w+)\s*$/;
Afterthisstatement,$1willholdthefirstwordand$2willholdthesecondword,assumingthat
thepatternmatches.The\s*metacharactersequencewillmatchanyleadingortrailing
whitespace.
Ifyouneedtoassignthefirsttwowordsinastringto$oneand$twoandtherestofthestringto
$rest,youcandothis:
$_="ThisisthewaytoSanJose.";
$word='\w+';#matchawholeword.
$space='\W+';#matchatleastonecharacterofwhitespace
$string='.*';#matchanynumberofanythingexcept
#forthenewlinecharacter.
($one,$two,$rest)=(m/^($word)$space($word)$space($string)/x);
Afterthisstatement,$onewillholdthefirstword,$twowillholdthesecondword,and$rest
willholdeverythingelseinthe$_variable.Thisexampleusesvariableinterpolationto,
hopefully,makethematchpatterneasiertoread.Thistechniquealsoemphasizeswhichmeta
sequenceisusedtomatchwordsandwhitespace.Itletsthereaderfocusonthewholeofthe
patternratherthantheindividualpatterncomponentsbyaddingalevelofabstraction.
Ifyouneedtoseeif$_containsalegalPerlvariablename,youcandothis:
$result=m/
^(?#Anchorthepatterntothestartofthestring)
[\$\@\%](?#Useacharacterclasstomatchthefirst)
(?#characterofavariablename)
[az](?#Useacharacterclasstoensurethatthe)
(?#characterofthenameisaletter)
\w*(?#Useacharacterclasstoensurethatthe)
(?#restofthevariablenameiseitheran)
(?#alphanumericoranunderscorecharacter)
$(?#Anchorthepatterntotheendofthe)
(?#string.Thismeansthatforthepatternto)
(?#match,thevariablenamemustbetheonly)
(?#valuein$_.)
/ix;#Usethe/ioptionsothatthesearchis
#caseinsensitiveandusethe/xoptionto
#allowextensions.
http://affy.blogspot.my/p5be/ch10.htm
23/30
11/6/2015
Perl5byExample:RegularExpressions
Afterthisstatement,$resultwillbetrueif$_containsalegalvariablenameandfalseifitdoes
not.
Ifyouneedtoseeif$_containsalegalintegerliteral,youcandothis:
$result=m/
(?#Firstcheckforjustnumbersin$_)
^(?#Anchortothestartofthestring)
\d+(?#Matchoneormoredigits)
$(?#Anchortotheendofthestring)
|(?#or)
(?#Nowcheckforhexadecimalnumbers)
^(?#Anchortothestartofthestring)
0x(?#The"0x"sequencestartsahexadecimalnumber)
[\daf]+(?#Matchoneormorehexadecimalcharacters)
$(?#Anchortotheendofthestring)
/ix;
Afterthisstatement,$resultwillbetrueif$_containsanintegerliteralandfalseifitdoesnot.
Ifyouneedtomatchalllegalintegersin$_,youcandothis:
@results=m/\d+$|^0[x][\daf]+/gi;
Afterthisstatement,@resultwillcontainalistofallintegerliteralsin$_.@resultwillcontain
anemptylistifnoliteralswerefound.
Ifyouneedtomatchtheendofthefirstwordinastring,youcandothis:
m/\w\W/;
Afterthisstatementisexecuted,$&willholdthelastcharacterofthefirstwordandthenext
characterthatfollowsit.Ifyouwantonlythelastcharacter,usepatternmemory,m/(\w)\W/;.
Then$1willbeequaltothelastcharacterofthefirstword.Ifyouusetheglobaloption,@array
=m/\w\W/g;,thenyoucancreateanarraythatholdsthelastcharacterofeachwordinthe
string.
Ifyouneedtomatchthestartofthesecondwordinastring,youcandothis:
m/\W\w/;
Afterthisstatement,$&willholdthefirstcharacterofthesecondwordandthewhitespace
characterthatimmediatelyprecedesit.Whilethispatternistheoppositeofthepatternthat
matchestheendofwords,itwillnotmatchthebeginningofthefirstword!Thisisbecauseof
the\Wmetacharacter.Simplyaddinga*metacharactertothepatternafterthe\Wdoesnothelp,
becausethenitwouldmatchonzerononwordcharactersandthereforematcheveryword
characterinthestring.
Ifyouneedtomatchthefilenameinafilespecification,youcandothis:
$_='/user/Jackie/temp/names.dat';
m!^.*/(.*)!;
Afterthismatchstatement,$1willequalnames.dat.Thematchisanchoredtothebeginningof
thestring,andthe.*componentmatcheseverythinguptothelastslashbecauseregular
expressionsaregreedy.Thenthenext(.*)matchesthefilenameandstoresitintopattern
http://affy.blogspot.my/p5be/ch10.htm
24/30
11/6/2015
Perl5byExample:RegularExpressions
memory.Youcanstorethefilepathintopatternmemorybyplacingparenthesesaroundthefirst
.*component.
Ifyouneedtomatchtwoprefixesandonerootword,like"rockfish"and"monkfish,"youcan
dothis:
m/(?:rock|monk)fish/x;
Thealternativemetacharacterisusedtosaythateitherrockormonkfollowedbyfishneedsto
befound.Ifyouneedtoknowwhichalternativewasfound,thenuseregularparenthesesinthe
pattern.Afterthematch,$1willbeequaltoeitherrockormonk.
Ifyouwanttosearchafileforastringandprintsomeofthesurroundinglines,youcandothis:
#readthewholefileintomemory.
open(FILE,"<fndstr.dat");
@array=<FILE>;
close(FILE);
#specifywhichstringtofind.
$stringToFind="A";
#iterateoverthearraylookingforthe
#string.The$#arraynotationisusedto
#determinethenumberofelementsinthe
#array.
for($index=0;$index<=$#array;$index++){
lastif$array[$index]=~/$stringToFind/;
}
#Use$indextoprinttwolinesbefore
#andtwolinesafterthelinethatcontains
#thematch.
foreach(@array[$index2..$index+2]){
print("$index:$_");
$index++;
}
Therearemanywaystoperformthistypeofsearch,andthisisjustoneofthem.Thistechnique
isonlygoodforrelativelysmallfilesbecausetheentirefileisreadintomemoryatonce.In
addition,theprogramassumesthattheinputfilealwayscontainsthestringthatyouarelooking
for.
Example:UsingtheSubstitutionOperator
Ifyouneedtoremovewhitespacefromthebeginningofastring,youcandothis:
s/^\s+//;
Thispatternusesthe\spredefinedcharacterclasstomatchanywhitespacecharacter.Theplus
signmeanstomatchoneormorewhitespacecharacters,andthecaretmeansmatchonlyatthe
beginningofthestring.
Ifyouneedtoremovewhitespacefromtheendofastring,youcandothis:
s/\s+$//;
Thispatternusesthe\spredefinedcharacterclasstomatchanywhitespacecharacter.Theplus
signmeanstomatchoneormorewhitespacecharacters,andthedollarsignmeansmatchonly
attheendofthestring.
http://affy.blogspot.my/p5be/ch10.htm
25/30
11/6/2015
Perl5byExample:RegularExpressions
Ifyouneedtoaddaprefixtoastring,youcandothis:
$prefix="A";
s/^(.*)/$prefix$1/;
Whenthesubstitutionisdone,thevalueinthe$prefixvariablewillbeaddedtothebeginning
ofthe$_variable.Thisisdonebyusingvariableinterpolationandpatternmemory.Ofcourse,
youmightalsoconsiderusingthestringconcatenationoperatorforinstance,$_="A".$_;,
whichisprobablyfaster.
Ifyouneedtoaddasuffixtoastring,youcandothis:
$suffix="Z";
s/^(.*)/$1$suffix/;
Whenthesubstitutionisdone,thevalueinthe$suffixvariablewillbeaddedtotheendofthe
$_variable.Thisisdonebyusingvariableinterpolationandpatternmemory.Ofcourse,you
mightalsoconsiderusingthestringconcatenationoperatorforinstance,$_.="Z";,whichis
probablyfaster.
Ifyouneedtoreversethefirsttwowordsinastring,youcandothis:
s/^\s*(\w+)\W+(\w+)/$2$1/;
Thissubstitutionstatementusesthepatternmemoryvariables$1and$2toreversethefirsttwo
wordsinastring.Youcanuseasimilartechniquetomanipulatecolumnsofinformation,the
lasttwowords,oreventochangetheorderofmorethantwomatches.
Ifyouneedtoduplicateeachcharacterinastring,youcandothis:
s/\w/$&x2/eg;
Whenthesubstitutionisdone,eachcharacterin$_willberepeated.Iftheoriginalstringwas
"123abc",thenewstringwouldbe"112233aabbcc".Theeoptionisusedtoforceevaluationof
thereplacementstring.The$&specialvariableisusedinthereplacementpatterntoreference
thematchedstring,whichisthenrepeatedbythestringrepetitionoperator.
Ifyouneedtocapitalizeallthewordsinasentence,youcandothis:
s/(\w+)/\u$1/g;
Whenthesubstitutionisdone,eachcharacterin$_willhaveitsfirstlettercapitalized.The/g
optionmeansthateachwordthe\w+metasequencewillbematchedandplacedin$1.Then
itwillbereplacedby\u$1.The\uwillcapitalizewhateverfollowsitinthiscase,it'sthe
matchedword.
Ifyouneedtoinsertastringbetweentworepeatedcharacters,youcandothis:
$_="!!!!";
$char="!";
$insert="AAA";
s{
($char)#lookforthespecifiedcharacter.
(?=$char)#lookforitagain,butdon'tinclude
#itthematchedstring,sothenext
}#searchwillalsofindit.
{
$char.$insert#concatenatethespecifiedcharacter
http://affy.blogspot.my/p5be/ch10.htm
26/30
11/6/2015
Perl5byExample:RegularExpressions
#withthestringtoinsert.
}xeg;#useextendedmode,evaluatethe
#replacementpattern,andmatchall
#possiblestrings.
print("$_\n");
Thisexampleusestheextendedmodetoaddcommentsdirectlyinsidetheregularexpression.
Thismakesiteasytorelatethecommentdirectlytoaspecificpatternelement.Thematch
patterndoesnotdirectlyreflecttheoriginallystatedgoalofinsertingastringbetweentwo
repeatedcharacters.Instead,theexamplewasquietlyrestated.Thenewgoalistosubstituteall
instancesof$charwith$char.$insert,if$charisfollowedby$char.Asyoucansee,theend
resultisthesame.Rememberthatsometimesyouneedtothinkoutsidethebox.
Ifyouneedtodoasecondlevelofvariableinterpolationinthereplacementpattern,youcando
this:
s/(\$\w+)/$1/eeg;
Thisisasimpleexampleofsecondaryvariableinterpolation.If$firstVar="AAA"and$_=
'$firstVar',then$_wouldbeequalto"AAA"afterthesubstitutionwasmade.Thekeyisthat
thereplacementpatternisevaluatedtwice.Thistechniqueisverypowerful.Itcanbeusedto
developerrormessagesusedwithvariableinterpolation.
$errMsg="Filetoolarge";
$fileName="DATA.OUT";
$_='Error:$errMsgforthefilenamed$fileName';
s/(\$\w+)/$1/eeg;
print;
Whenthisprogramisrun,itwilldisplay
Error:FiletoolargeforthefilenamedDATA.OUT
Thevaluesofthe$errMsgand$fileNamevariableswereinterpolatedintothereplacement
patternasneeded.
Example:UsingtheTranslationOperator
Ifyouneedtocountthenumberoftimesagivenletterappearsinastring,youcandothis:
$cnt=tr/Aa//;
Afterthisstatementexecutes,$cntwillholdthenumberoftimestheletteraappearsin$_.The
troperatordoesnothaveanoptiontoignorethecaseofthestring,sobothupperand
lowercaseneedtobespecified.
Ifyouneedtoturnthehighbitoffforeverycharacterin$_,youcandothis:
tr[\200\377][\000\177];
Thisstatementusesthesquarebracketstodelimitthecharacterlists.Noticethatspacescanbe
usedbetweenthepairsofbracketstoenhancereadabilityofthelists.Theoctalvaluesareused
tospecifythecharacterranges.Thetranslationoperatorismoreefficientinthisinstancethan
usinglogicaloperatorsandaloopstatement.Thisisbecausethetranslationcanbedoneby
creatingasimplelookuptable.
http://affy.blogspot.my/p5be/ch10.htm
27/30
11/6/2015
Perl5byExample:RegularExpressions
Example:UsingtheSplit()Function
Ifyouneedtosplitastringintowords,youcandothis:
s/^\s+//;
@array=split;
Afterthisstatementexecutes,@arraywillbeanarrayofwords.Beforesplittingthestring,you
needtoremoveanybeginningwhitespace.Ifthisisnotdone,splitwillcreateanarrayelement
withthewhitespaceasthefirstelementinthearray,andthisisprobablynotwhatyouwant.
Ifyouneedtosplitastringcontainedin$lineinsteadof$_intowords,youcandothis:
$line=~s/^\s+//;
@array=split(/\W/,$line);
Afterthisstatementexecutes,@arraywillbeanarrayofwords.
Ifyouneedtosplitastringintocharacters,youcandothis:
@array=split(//);
Afterthisstatementexecutes,@arraywillbeanarrayofcharacters.splitrecognizestheempty
patternasarequesttomakeeverycharacterintoaseparatearrayelement.
Ifyouneedtosplitastringintofieldsbasedonadelimitersequenceofcharacters,youcando
this:
@array=split(/:/);
@arraywillbeanarrayofstringsconsistingofthevaluesbetweenthedelimiters.Ifthereare
repeateddelimiters::inthisexamplethenanemptyarrayelementwillbecreated.Use/:+/
asthedelimitertomatchinordertoeliminatetheemptyarrayelements.
Summary
Thischapterintroducedyoutoregularexpressionsorpatterns,regularexpressionoperators,andthe
bindingoperators.Therearethreeregularexpressionoperatorsm//,s///,andtr///whichareused
tomatch,substitute,andtranslateandusethe$_variableasthedefaultoperand.Thebinding
operators,=~and!~,areusedtobindtheregularexpressionoperatorstoavariableotherthan$_.
Whiletheslashcharacteristhedefaultpatterndelimiter,youcanuseanycharacterinitsplace.This
featureisusefulifthepatterncontainstheslashcharacter.Ifyouuseanopeningbracketor
parenthesisasthebeginningdelimiter,usetheclosingbracketorparenthesisastheendingdelimiter.
Usingthesinglequoteasthedelimiterwillturnoffvariableinterpolationforthepattern.
Thematchingoperatorhassixoptions:/g,/i,/m,/o,/s,and/x.Theseoptionsweredescribedin
Table10.2.I'vefoundthatthe/xoptionisveryhelpfulforcreatingmaintainable,commented
programs.The/goption,usedtofindallmatchesinastring,isalsoveryuseful.And,ofcourse,the
capabilitytocreatecaseinsensitivepatternsusingthe/ioptioniscrucialinmanycases.
Thesubstitutionoperatorhasthesameoptionsasthematchingoperatorandonemorethe/eoption.
The/eoptionletsyouevaluatethereplacementpatternandusethenewvalueasthereplacement
string.Ifyouusebackquotesasdelimiters,thereplacementpatternwillbeexecutedasaDOSor
UNIXcommand,andtheresultingoutputwillbecomethereplacementstring.
http://affy.blogspot.my/p5be/ch10.htm
28/30
11/6/2015
Perl5byExample:RegularExpressions
Thetranslationoperatorhasthreeoptions:/c,/d,and/s.Theseoptionsareusedtocomplementthe
matchcharacterlist,deletecharactersnotinthematchcharacterlist,andeliminaterepeatedcharacters
inastring.Ifnoreplacementlistisspecified,thenumberofmatchedcharacterswillbereturned.This
ishandyifyouneedtoknowhowmanytimesagivencharacterappearsinastring.
Thebindingoperatorsareusedtoforcethematching,substitution,andtranslationoperatorstosearch
avariableotherthan$_.The=~operatorcanbeusedwithallthreeoftheregularexpressionoperators,
whilethe!~operatorcanbeusedonlywiththematchingoperator.
Quiteabitofspacewasdevotedtocreatingpatterns,andthetopicdeservesevenmorespace.Thisis
easilyoneofthemoreinvolvedfeaturesofthePerllanguage.Onekeyconceptisthatacharactercan
havemultiplemeanings.Forexample,theplussigncanmeanaplussigninoneinstance(itsliteral
meaning),andinanotheritmeansmatchsomethingoneormoretimes(itsmetameaning).
Youlearnedaboutregularexpressioncomponentsandthattheycanbecombinedinaninfinite
numberofways.Table10.5listedmostofthemetameaningsfordifferentcharacters.Youreadabout
characterclasses,alternation,quantifiers,anchors,patternmemory,wordboundaries,andextended
components.
Thelastsectionofthechapterwasdevotedtopresentingnumerousexamplesofhowtouseregular
expressionstoaccomplishspecificgoals.Eachsituationwasdescribed,andapatternthatmatched
thatsituationwasshown.Somecommentarywasgivenforeachexample.
Inthenextchapter,you'llreadabouthowtopresentinformationbyusingformats.Formatsareused
tohelprelievesomeoftheprogrammingburdenfromthetaskofcreatingreports.
ReviewQuestions
1. Canyouusevariableinterpolationwiththetranslationoperator?
2. Whathappensifthepatternisempty?
3. Whatvariabledoesthesubstitutionoperatoruseasitsdefault?
4. Willthefollowinglineofcodework?
m{.*];
5. Whatisthe/goptionofthesubstitutionoperatorusedfor?
6. Whatdoesthe\dmetacharactersequencemean?
7. Whatisthemeaningofthedollarsigninthefollowingpattern?
/AA[.<]$]ER/
8. Whatisawordboundary?
9. Whatwillbedisplayedbythefollowingprogram?
$_='ABABAC';
printm/c$/i;
ReviewExercises
http://affy.blogspot.my/p5be/ch10.htm
29/30
11/6/2015
Perl5byExample:RegularExpressions
1. Writeapatternthatmatcheseither"top"or"topgun".
2. WriteaprogramthatacceptsinputfromSTDINandchangesallinstancesoftheletteraintothe
letterb.
3. Writeapatternthatstoresthefirstcharactertofollowatabintopatternmemory.
4. Writeapatternthatmatchesthelettergbetween3and7times.
5. Writeaprogramthatfindsrepeatedwordsinaninputfileandprintstherepeatedwordandthe
linenumberonwhichitwasfound.
6. Createacharacterclassforoctalnumbers.
7. Writeaprogramthatusesthetranslationoperatortoremoverepeatedinstancesofthetab
characterandthenreplacesthetabcharacterwithaspacecharacter.
8. Writeapatternthatmatcheseither"top"or"topgun"usingazerowidthpositivelookahead
assertion.
TopofPage|Sections|Chapters|Copyright
http://affy.blogspot.my/p5be/ch10.htm
30/30