Perl - Regular Expressions - Tutorialspoint
文章推薦指數: 80 %
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>
延伸文章資訊
- 1Perl Regular Expression(正規表示式) - Totui - 痞客邦
Perl Regular Expression(正規表示式) ; g, Match globally, i.e. find all occurrences. ; i, Makes the sea...
- 2perlre - Perl regular expressions - Perldoc Browser
DESCRIPTION. This page describes the syntax of regular expressions in Perl. If you haven't used r...
- 3Perl | Regular Expressions - GeeksforGeeks
- 4Perl | Regex Cheat Sheet - GeeksforGeeks
Regex or Regular Expressions are an important part of Perl Programming. It is used for searching ...
- 5精簡扼要的Perl 課程講義(六):常規表達式(Regular ...
常規表達式(一) (Regular expression) # (1) 基本樣式比對"=~" 與"!~" # 比對字串,成功傳回true # 失敗傳回false "Hello World" =~...