Perl - Regular Expressions - Tutorialspoint

文章推薦指數: 80 %
投票人數:10人

A regular expression is a string of characters that defines the pattern or patterns you are viewing. The syntax of regular expressions in Perl is very ... Home CodingGround Jobs Whiteboard Tools Business Teachwithus PerlBasics Perl-Home Perl-Introduction Perl-Environment Perl-SyntaxOverview Perl-DataTypes Perl-Variables Perl-Scalars Perl-Arrays Perl-Hashes Perl-IF...ELSE Perl-Loops Perl-Operators Perl-Date&Time Perl-Subroutines Perl-References Perl-Formats Perl-FileI/O Perl-Directories Perl-ErrorHandling Perl-SpecialVariables Perl-CodingStandard Perl-RegularExpressions Perl-SendingEmail PerlAdvanced Perl-SocketProgramming Perl-ObjectOriented Perl-DatabaseAccess Perl-CGIProgramming Perl-Packages&Modules Perl-ProcessManagement Perl-EmbeddedDocumentation Perl-FunctionsReferences PerlUsefulResources Perl-QuestionsandAnswers Perl-QuickGuide Perl-UsefulResources Perl-Discussion SelectedReading UPSCIASExamsNotes Developer'sBestPractices QuestionsandAnswers EffectiveResumeWriting HRInterviewQuestions ComputerGlossary WhoisWho Perl-RegularExpressions Advertisements PreviousPage NextPage  Aregularexpressionisastringofcharactersthatdefinesthepatternorpatternsyouareviewing.ThesyntaxofregularexpressionsinPerlisverysimilartowhatyouwillfindwithinotherregularexpression.supportingprograms,suchassed,grep,andawk. Thebasicmethodforapplyingaregularexpressionistousethepatternbindingoperators=~and!~.Thefirstoperatorisatestandassignmentoperator. TherearethreeregularexpressionoperatorswithinPerl. MatchRegularExpression-m// SubstituteRegularExpression-s/// TransliterateRegularExpression-tr/// Theforwardslashesineachcaseactasdelimitersfortheregularexpression(regex)thatyouarespecifying.Ifyouarecomfortablewithanyotherdelimiter,thenyoucanuseinplaceofforwardslash. TheMatchOperator Thematchoperator,m//,isusedtomatchastringorstatementtoaregularexpression.Forexample,tomatchthecharactersequence"foo"againstthescalar$bar,youmightuseastatementlikethis− LiveDemo #!/usr/bin/perl $bar="Thisisfooandagainfoo"; if($bar=~/foo/){ print"Firsttimeismatching\n"; }else{ print"Firsttimeisnotmatching\n"; } $bar="foo"; if($bar=~/foo/){ print"Secondtimeismatching\n"; }else{ print"Secondtimeisnotmatching\n"; } Whenaboveprogramisexecuted,itproducesthefollowingresult− Firsttimeismatching Secondtimeismatching Them//actuallyworksinthesamefashionastheq//operatorseries.youcanuseanycombinationofnaturallymatchingcharacterstoactasdelimitersfortheexpression.Forexample,m{},m(),andm> Greedyrepetition:matches"perl>" 2 <.> Nongreedy:matches""in"perl>" GroupingwithParentheses Sr.No. Example&Description 1 \D\d+ Nogroup:+repeats\d 2 (\D\d)+ Grouped:+repeats\D\dpair 3 ([Pp]ython(,)?)+ Match"Python","Python,python,python",etc. Backreferences Thismatchesapreviouslymatchedgroupagain− Sr.No. Example&Description 1 ([Pp])ython&\1ails Matchespython&pailsorPython&Pails 2 (['"])[^\1]*\1 Singleordouble-quotedstring.\1matcheswhateverthe1stgroupmatched.\2matcheswhateverthe2ndgroupmatched,etc. Alternatives Sr.No. Example&Description 1 python|perl Matches"python"or"perl" 2 rub(y|le)) Matches"ruby"or"ruble" 3 Python(!+|\?) "Python"followedbyoneormore!orone? Anchors Thisneedtospecifymatchpositions. Sr.No. Example&Description 1 ^Python Matches"Python"atthestartofastringorinternalline 2 Python$ Matches"Python"attheendofastringorline 3 \APython Matches"Python"atthestartofastring 4 Python\Z Matches"Python"attheendofastring 5 \bPython\b Matches"Python"atawordboundary 6 \brub\B \Bisnonwordboundary:match"rub"in"rube"and"ruby"butnotalone 7 Python(?=!) Matches"Python",iffollowedbyanexclamationpoint 8 Python(?!!) Matches"Python",ifnotfollowedbyanexclamationpoint SpecialSyntaxwithParentheses Sr.No. Example&Description 1 R(?#comment) Matches"R".Alltherestisacomment 2 R(?i)uby Case-insensitivewhilematching"uby" 3 R(?i:uby) Sameasabove 4 rub(?:y|le)) Grouponlywithoutcreating\1backreference PreviousPage PrintPage NextPage  Advertisements



請為這篇文章評分?