It's greatforpracticingandtestingoutregularexpressions, so I highlyrecommendyougodowntothelinkinthedescriptionandfollowalongonthissiteandnowtogetstarted.
I firstwanttoexplainwhatregularexpressionsare.
Andessentially a regularexpressionis a waytosearchthrough a stringoftext.
Sowehavethistextinhereandatthetop.
Wehave a regularexpression, andwiththatregularexpressionallowsustodoisitallowsustosearchthroughthattextinordertodothingssuchasvalidationorgetcertainpiecesofthetextwecandoadvancedfindinreplace.
There's a tonofthingsthatyoucoulddowithregularexpression.
Butingeneral, allthatregularexpressionisdoingisitallowsyoutosearchtheirtextin a reallyadvancedwayandevengroupthosedifferentsearchestogethersoyoucoulddopowerfulthingssuchasfindingyourplacewithit.
Sotogetstarted, I kindofwanttobreakdowntheformatof a regularexpression.
Asyoucanseeuphere, wehaveourregularexpression, andinthemiddleherewehaveourtextweresearchingandregularexpressionisalmostalwaysgonnastartwiththisopeningslashsharethisforwardslash, andit's goingtoendwith a FordSlashaswell.
SoeverythingbetweentheFordslashesisgoingtobeyourregularexpression, thenalsosomethingthat's a partof a regularexpressionisthepartaftertheforwardslashhereattheendtheseair, thedifferentflags.
So, forexample, if I putthewith a capital T h hereandthenif I makethisglobal, youcanseeit's matchingbothofthesethoseAndthat's because I havecaseinsensitiveturnedon.
If I turnthatoff, it's onLeegonnamatchthethatiscapitalized.
Andifwechangesto a lowercase, it's onlygoingtomatchthelowercaseone.
If I typeinformationintohere, whatevertypeisgoingtobesearchedforinthestringdownhere, forexample, at I cansearchforFatand I'm justgoingtogetthewordfatdoranandsoon, anditworksjustlikenormalfinding a placewhichisnotreallythatpowerful.
Butwheredoesbecomepowerfuliswhenyoucanactuallystartaddingintheregularexpressionthings, andthevery 1st 1 that I wanttotalkaboutistheideaofthesedifferentspecialcharactersthatallowyoutodocertainthings.
So, forexample, wecansearchforthecharacter E.
Andyoucanseewegeteverysingle e insideofoursearch.
Butwhatifwewantedtosearchfor a stringofthesesomultipleeasein a row?
Wecouldput E andthenwecouldput a plus.
Andwhatthisissaying, Isitsayingmatch E?
Butwewant a match.
Atleastone.
Thisisoneormoreoftheproceedingtoken, sowe'resayingMatch E ormorethanone e.
Soyoucanseewehavethisbabyandmatchthis e.
ButthisisactuallymatchingbothofArtie's becausethere's two e's in a rowandtheplusesjustsayingmatchasmanyin a rowasyoucanbutmatchatleastoneandthenanothercharacterthatwecando.
Forexample, wecanputin a inhereandwecanput a questionmark, andthat's sayingthatthisisoptionalsooptionally I wanttogetthis a character.
Soherewehave e A.
Soit's goingtogetthecharacter E asyoucanseeinallofthese, andthenifthere's an A, it's alsogoingtobashit.
Sothisquestionmarksayszeroorone.
I liketothinkofitaswhateverisbefore.
Itisoptional.
Sonarcaseare a isoptional, andwhereverthere's an A afteran E, it's matchingit.
Butinthecasewherethere's nowayaftertheit's justcompletelyignoringit, thenextone I wanttotalkaboutisgoingtobehere.
Andif, forexample, wewantedtosearchforteaandthenwewanttosearchfor a periodandwewanttosearchforanotherperiod, youcanseethatit's notactuallywrappingthenewlinebecauseperiodcan't match a newline.
Wecanput a minimumand a maximumnumberforhowlargewewantsomething.
So, forexample, let's saywewantedtogetallthewordsthatareoverfourcharacters.
Wecouldsayfourandthisisgoingtogetusanyfourdigitsin a row, andifweput a comma, it's goingtosay, Getanyfourdigitsormore.
Ifyouwantedtoget 4 to 5 characters, wewouldsay 4 to 5 andisgoingtomatchanysetofcharactersthatarebetweenfourandfivelongandthatrightthereisprettymuchthemajorityofallofyourcountingselectorsaswellasyourspecialcharacterselectorsthatyouhavetoworryabout.
Butanotherthingthatyoucandoisactuallydocharactergroupingssowecangetallofthecharactersinsideof a group.
So, forexample, let's saythatwewantedtogetanysinglewordthatendsin a tea, andwewantedtoget a threeletterwordthatendsin a tea, andwewantedtogetonlythewordsthatstartwitheither F orseewhatwecoulddohereasinsideofsquarebrackets.
Weputanycharactersthatwewant a match, andit's saying, matchanyofthecharactersinsideofhere, forexample, it's goingtomatchanythingthatstartswith F or a C andendswithan A T.
Sowecould, forexample, a dizzyandthisisgoingtobeallofthecharactersbetween A and C.
WecouldalsodobetweenCapital A andCapital Z, andnowwehaveallofthecharactersbetween A and Z thatarelowercaseandallthecharactersbetween A and Z Betteruppercase.
But I'm goingtobecheckedinside.
Thisregularexpressioncoulddothesamething, forexample, with 0 to 9 ifwewantedto.
Forexample, wecoulddolike a deaf, andit's goingtogetanythingbetween A and F Wecoulddo a to C, andwe'rejustgonnagetcatnow.
Inthatcase, somethingelsethat's reallyusefulistheabilitytoputthingsinsideof a group, andthat's whatweuseParenthesesforanythinginsideoftheparenthesesaregoingtobetheirowngroup, andtheyonlyactuponthemselvessoForexample, wecouldsearchforteaoruppercase T lips, uppercase T.
AndwithvisitDoesthislittlepipecharacterhere?
IsitSaintmatchAnyofthese?
Soit's like a orresultissayingeitherlowercase T oruppercase T.
Thealsosomethingthat's reallyusefulwiththesegroupsiswecanactuallydosomethingthingssuchasourmatterswherewecouldsaywewant a match, forexample, 2 to 3, andit's goingtosaywewant a match 2 to 3 ofthischaractergroupinhere.
Soyou'reoverCase T orupperCase T.
AndsowecanactuallyseethisineffectbydoingIfwewant t e and R, youcanseeit's goingtomatchbetweentwoandthreeofthese.
Andwecouldputyouknow, forexample, thatandit's gonnamatchthat a wholeword, thebutonlygoingtomatchthetheverybeginningevenifwemadeitsothatitcouldbe a lowercase t bydoingthissoitcouldbeeitheruppercaseteaorlowercase T.
I'm gonnatalkabout a lookbehindfirst, andwe'regonnatalkaboutthepositivelookbehind, andthewaythatyouuse a positivelookbehindisyouneedtoputitinsideof a group, andthisgroupisgoingtostartwith a questionmarkaswellisthelessthansignandanequal.
So, forexample, whatifwewanttogeteverysinglewordthatisfollowedbythewordtheWecancomeinhereandwecansaythatwewanttomatcheither a teaoruppercasetea, andwewanttomatch H E.
Andwecanjustsaywewanttheveryfirstcharacterthat's followedbythewordtheSowe'lljustput a periodhereandyoucanseewe'regettingbothofthespacesthatairfollowedbytheButwe'renotactuallygettingthewordtheinsideofourselection.
Replaceallthiswithjust a randomphonenumberwilljustsay 1234567890 Sowehaveourareacodeofthreedigitsandthreemoredigitsandthenourlastfourdigits.
Thisisgoingtobetheeasiestphonenumbertocheckfor, becauseallweneedtodoisjustcheckforninecharactersin a row, sowecouldjustsayslash d whichisgoingtobefordigits.
Thisisanyformofnumberdigitandwecouldjustsaythatwewanttogetnineofthemin a row.
I'm sorry.
Weneedtomakesureweget 10 ofthemin a rowbecauseis a 10 digitnumber, andasyoucansee, that's matchingthatperfectly.