54
Anchors:Awaytotrimemptiness
61
Usingthe
$
,however,requiresnoextrareplacementaction.Itsimplyassertsthatthepatterncontains
theend-of-the-line,whichisn’tanactualcharacter,perse,inthetext.
Inotherwords,the
$
onlydetectstheend-of-the-line,andsonoreplacementisactuallydoneatthat
position.
Try it out yourself. In your Find field, match the
$
operator and then replace itwith nothing:
nothingwillhappentothetext(comparethistoreplacing
\n
withnothing).
Solet’sstripthetrailingcommasusingthe
$
operator:
Find
,$
Replace (withnothing)
InEnglish Wewanttofindthecommarightbeforetheend-of-the-line
Escapingspecial characters
Ifthecaretandthedollarsigndenotethebeginningandtheendoftheline, respectively,thenit
followsthatthefollowingpatternsdon’tmakemuchsense:
abc^Bye
Hello$world
Howwould
abc
comebeforethebeginningofthelineyetbeonthesamelineas
Bye
?Orhowcould
world
beonthesamelineas
Hello
andtheendofthelinecharacter,
$
?
So,withwhatweknowsofar,itmakessensethatthecaretanddollarsigntypicallyserveasbookends
foraregexpattern. However,thefollowingregexmaybeconfusing:
He gave me e \$10+
Thisregexmatchesthetextinboldbelow:
Hegaveme$100yesterday
Thekeycharacterhereisourfriendthebackslash. We’veseenhowthebackslash,whenpreceding
theletters
b
and
n
,givethemspecialmeaning:awordboundaryandnewlinecharacter,respectively.
Whathappenswhenthebackslashprecedesanalready specialcharacter,suchasthedollar sign?
Thenit’sjustaliteraldollarsign,asweseeintheaboveexample.
Exercise: Removeleadingdollarsigns Giventhislistofdollaramountsinwhichthedollarsign
ismistakenlyrepeated: