SlideShare uma empresa Scribd logo
1 de 13
Regular expressions
Pattern matching with Perl scripting language




http://arraylist.blogspot.com
Regular expressions
  We usually talk about regular expressions and
  pattern matching within the context of scripting
  language such as Perl or Shell script.

  Lets us look at pattern matching using regular
  expression with Perl scripting language




http://arraylist.blogspot.com
Regular expressions
  Pattern matching in Perl occurs using a match
  operator such as
  m// or m:: or m,,

  Example – m/simple/

  Here the text “simple” is matched against ? - $_

  $_ is the default scalar variable in Perl.




http://arraylist.blogspot.com
Regular expressions
  Metacharacters have to be preceded with a 
  during pattern matching.

  Metacharacters ^ $ ( )  | @ [ { ? . + *

  So to match m/$10/ we write m/$10/




http://arraylist.blogspot.com
Regular expressions
  m// if we use // as delimiters – we can avoid
  the character m during pattern matching.
  So m/simple/ can be /simple/

  To match variables using regex simply use /
  $varname/




http://arraylist.blogspot.com
Regular expressions
  Metacharacters . ^ $ ( )  | @ [ { ? + *

  .  Matches a single character
  Example /d.t/ matches dot, dit, d t

  If we want . to behave as a non-metacharacter
  we preceed it with a 

  Thus /d.t/ matches d.t




http://arraylist.blogspot.com
Regular expressions
  Metacharacters . ^ $ ( )  | @ [ { ? + *

  Special characters
  n – newline
  r – carriage return
  t – tab
  f – formfeed

  Special characters take the same meaning
  inside // during pattern matching




http://arraylist.blogspot.com
Regular expressions
  Quantifiers – tells the regex , how many times a
  pattern should be matched.
  “+” match minimum once or as many times as it
  occurs
  Example /go+d/ matches good but not god
  “*” matches preceding character 0 or more times

  Example /hik*e/ matches hike, hie – matches k 0
  or more times between hi and e

  “?” matches preceding character 0 or 1 times but
  not more.
  Example /h?ello/ matches hello or ello but not
  hhello



http://arraylist.blogspot.com
Regular expressions
  {} matched characters specified number of times
  /a{5,10}/ - matches the character a at least 5
  times ,
  but no more than 10 times
  /a{5,}/ - matches 5 and more times.
  /a{0,2}/ - matches 0 or at the most 2 times.
  /a{5}/ - match exactly six times

  .* - matches anything between 2 set of characters
  /hello.*world/ matches “hello Joe welcome to the
  world”




http://arraylist.blogspot.com
Regular expressions
  Square brackets [] and character class

  [abcd] – match any of the characters a, b, c, d
  [a-d] – also means the same thing
  [ls]Aa[rs] – match uppercase A or lowercase a
  [0-9] – match a digit
  [A-Za-z]{5} - match any group of 5 alphabetic
  characters
  [^a-z] - match all capital case letters - ^ is a
  negation
  [*!@#$%&()] - match any of these characters



http://arraylist.blogspot.com
Regular expressions
  Special Character classes

  w – match a word character same as [a-Za-z]
  W – match non-word characters
  d –match a digit [0-9]
  D- match a non-digit
  s - match a whitespace character
  S - match a non-whitespace character
  Example - /d{3}/ - match 3 digits
  /sw+s/ - match a group of words surrounded
  by white space


http://arraylist.blogspot.com
Regular expressions
  Alternation and Anchors
  Alternation uses | which means “or”
  Eg. /tea|coffee/  check if string contains tea or
  coffee
  Grouping with alternation
  Eg. /(fr|bl|cl)og/  if string contains frog or blog
  or clog

  Anchors let you tell where you want to look for a
  character
  ^ - caret .eg. /^tea/ matches tea only if it occurs
  at the beginning of the line
  $ - dollar sign .eg. /sample$/ matches sample only
  at the end of the line.



http://arraylist.blogspot.com
Regular expressions
  Substitution
  Syntax – s/// 
  s/searchstring/replacementstring/
  Eg. $_ = “lies does not make sense”
  s/lies/truth/  “truth does not make sense”

  Instead of / you can use # as a substitution
  operator
  Example . s#lies#truth#;




http://arraylist.blogspot.com

Mais conteúdo relacionado

Mais procurados

Perl programming language
Perl programming languagePerl programming language
Perl programming languageElie Obeid
 
Regular Expressions grep and egrep
Regular Expressions grep and egrepRegular Expressions grep and egrep
Regular Expressions grep and egrepTri Truong
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20Max Kleiner
 
Regular Expression
Regular ExpressionRegular Expression
Regular ExpressionLambert Lum
 
Tutorial on Regular Expression in Perl (perldoc Perlretut)
Tutorial on Regular Expression in Perl (perldoc Perlretut)Tutorial on Regular Expression in Perl (perldoc Perlretut)
Tutorial on Regular Expression in Perl (perldoc Perlretut)FrescatiStory
 
Lecture 23
Lecture 23Lecture 23
Lecture 23rhshriva
 
Regular Expressions 2007
Regular Expressions 2007Regular Expressions 2007
Regular Expressions 2007Geoffrey Dunn
 
CGI With Object Oriented Perl
CGI With Object Oriented PerlCGI With Object Oriented Perl
CGI With Object Oriented PerlBunty Ray
 
Regular Expressions in PHP
Regular Expressions in PHPRegular Expressions in PHP
Regular Expressions in PHPAndrew Kandels
 
P H P Part I, By Kian
P H P  Part  I,  By  KianP H P  Part  I,  By  Kian
P H P Part I, By Kianphelios
 
Operator precedance parsing
Operator precedance parsingOperator precedance parsing
Operator precedance parsingsanchi29
 

Mais procurados (17)

Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
 
Perl programming language
Perl programming languagePerl programming language
Perl programming language
 
perltut
perltutperltut
perltut
 
Regular Expressions grep and egrep
Regular Expressions grep and egrepRegular Expressions grep and egrep
Regular Expressions grep and egrep
 
Sed tips and_tricks
Sed tips and_tricksSed tips and_tricks
Sed tips and_tricks
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
 
Tutorial on Regular Expression in Perl (perldoc Perlretut)
Tutorial on Regular Expression in Perl (perldoc Perlretut)Tutorial on Regular Expression in Perl (perldoc Perlretut)
Tutorial on Regular Expression in Perl (perldoc Perlretut)
 
Lecture 23
Lecture 23Lecture 23
Lecture 23
 
Regular Expressions 2007
Regular Expressions 2007Regular Expressions 2007
Regular Expressions 2007
 
CGI With Object Oriented Perl
CGI With Object Oriented PerlCGI With Object Oriented Perl
CGI With Object Oriented Perl
 
Regular Expressions in PHP
Regular Expressions in PHPRegular Expressions in PHP
Regular Expressions in PHP
 
P H P Part I, By Kian
P H P  Part  I,  By  KianP H P  Part  I,  By  Kian
P H P Part I, By Kian
 
Operator precedance parsing
Operator precedance parsingOperator precedance parsing
Operator precedance parsing
 
C# slid
C# slidC# slid
C# slid
 
Javascript
JavascriptJavascript
Javascript
 
Perl_Part1
Perl_Part1Perl_Part1
Perl_Part1
 

Destaque (10)

CCNA part 4 routers
CCNA part 4 routersCCNA part 4 routers
CCNA part 4 routers
 
Sca n instructorppt_chapter1_final
Sca n instructorppt_chapter1_finalSca n instructorppt_chapter1_final
Sca n instructorppt_chapter1_final
 
Regexp master 2011
Regexp master 2011Regexp master 2011
Regexp master 2011
 
Erlang with Regexp Perl And Port
Erlang with Regexp Perl And PortErlang with Regexp Perl And Port
Erlang with Regexp Perl And Port
 
Regexp Master
Regexp MasterRegexp Master
Regexp Master
 
Working with text, Regular expressions
Working with text, Regular expressionsWorking with text, Regular expressions
Working with text, Regular expressions
 
El modelo económico de Maduro Fracasó
El modelo económico de Maduro FracasóEl modelo económico de Maduro Fracasó
El modelo económico de Maduro Fracasó
 
Mastering Regex in Perl
Mastering Regex in PerlMastering Regex in Perl
Mastering Regex in Perl
 
Browser Exploit Framework
Browser Exploit FrameworkBrowser Exploit Framework
Browser Exploit Framework
 
Perl Basics for Pentesters Part 1
Perl Basics for Pentesters Part 1Perl Basics for Pentesters Part 1
Perl Basics for Pentesters Part 1
 

Semelhante a Regular expressions in Perl

PERL Regular Expression
PERL Regular ExpressionPERL Regular Expression
PERL Regular ExpressionBinsent Ribera
 
Introduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_RIntroduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_RHellen Gakuruh
 
Introduction to regular expressions
Introduction to regular expressionsIntroduction to regular expressions
Introduction to regular expressionsBen Brumfield
 
Php Chapter 4 Training
Php Chapter 4 TrainingPhp Chapter 4 Training
Php Chapter 4 TrainingChris Chubb
 
Looking for Patterns
Looking for PatternsLooking for Patterns
Looking for PatternsKeith Wright
 
Regex startup
Regex startupRegex startup
Regex startupPayPal
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionssana mateen
 
Chapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular ExpressionChapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular Expressionazzamhadeel89
 
Regular Expressions in PHP, MySQL by programmerblog.net
Regular Expressions in PHP, MySQL by programmerblog.netRegular Expressions in PHP, MySQL by programmerblog.net
Regular Expressions in PHP, MySQL by programmerblog.netProgrammer Blog
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP StringsAhmed Swilam
 
Eloquent Ruby chapter 4 - Find The Right String with Regular Expression
Eloquent Ruby chapter 4 - Find The Right String with Regular ExpressionEloquent Ruby chapter 4 - Find The Right String with Regular Expression
Eloquent Ruby chapter 4 - Find The Right String with Regular ExpressionKuyseng Chhoeun
 
Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013Ben Brumfield
 
Basta mastering regex power
Basta mastering regex powerBasta mastering regex power
Basta mastering regex powerMax Kleiner
 
Javascript正则表达式
Javascript正则表达式Javascript正则表达式
Javascript正则表达式ji guang
 
Regular Expression in Action
Regular Expression in ActionRegular Expression in Action
Regular Expression in ActionFolio3 Software
 

Semelhante a Regular expressions in Perl (20)

PERL Regular Expression
PERL Regular ExpressionPERL Regular Expression
PERL Regular Expression
 
Introduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_RIntroduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_R
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
regex.ppt
regex.pptregex.ppt
regex.ppt
 
Introduction to regular expressions
Introduction to regular expressionsIntroduction to regular expressions
Introduction to regular expressions
 
Regex lecture
Regex lectureRegex lecture
Regex lecture
 
Regular Expressions in Stata
Regular Expressions in StataRegular Expressions in Stata
Regular Expressions in Stata
 
Php Chapter 4 Training
Php Chapter 4 TrainingPhp Chapter 4 Training
Php Chapter 4 Training
 
Looking for Patterns
Looking for PatternsLooking for Patterns
Looking for Patterns
 
Regex startup
Regex startupRegex startup
Regex startup
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressions
 
Chapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular ExpressionChapter 3: Introduction to Regular Expression
Chapter 3: Introduction to Regular Expression
 
Regular Expressions in PHP, MySQL by programmerblog.net
Regular Expressions in PHP, MySQL by programmerblog.netRegular Expressions in PHP, MySQL by programmerblog.net
Regular Expressions in PHP, MySQL by programmerblog.net
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
 
Eloquent Ruby chapter 4 - Find The Right String with Regular Expression
Eloquent Ruby chapter 4 - Find The Right String with Regular ExpressionEloquent Ruby chapter 4 - Find The Right String with Regular Expression
Eloquent Ruby chapter 4 - Find The Right String with Regular Expression
 
Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013
 
Ruby cheat sheet
Ruby cheat sheetRuby cheat sheet
Ruby cheat sheet
 
Basta mastering regex power
Basta mastering regex powerBasta mastering regex power
Basta mastering regex power
 
Javascript正则表达式
Javascript正则表达式Javascript正则表达式
Javascript正则表达式
 
Regular Expression in Action
Regular Expression in ActionRegular Expression in Action
Regular Expression in Action
 

Último

FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 

Último (20)

FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 

Regular expressions in Perl

  • 1. Regular expressions Pattern matching with Perl scripting language http://arraylist.blogspot.com
  • 2. Regular expressions We usually talk about regular expressions and pattern matching within the context of scripting language such as Perl or Shell script. Lets us look at pattern matching using regular expression with Perl scripting language http://arraylist.blogspot.com
  • 3. Regular expressions Pattern matching in Perl occurs using a match operator such as m// or m:: or m,, Example – m/simple/ Here the text “simple” is matched against ? - $_ $_ is the default scalar variable in Perl. http://arraylist.blogspot.com
  • 4. Regular expressions Metacharacters have to be preceded with a during pattern matching. Metacharacters ^ $ ( ) | @ [ { ? . + * So to match m/$10/ we write m/$10/ http://arraylist.blogspot.com
  • 5. Regular expressions m// if we use // as delimiters – we can avoid the character m during pattern matching. So m/simple/ can be /simple/ To match variables using regex simply use / $varname/ http://arraylist.blogspot.com
  • 6. Regular expressions Metacharacters . ^ $ ( ) | @ [ { ? + * . Matches a single character Example /d.t/ matches dot, dit, d t If we want . to behave as a non-metacharacter we preceed it with a Thus /d.t/ matches d.t http://arraylist.blogspot.com
  • 7. Regular expressions Metacharacters . ^ $ ( ) | @ [ { ? + * Special characters n – newline r – carriage return t – tab f – formfeed Special characters take the same meaning inside // during pattern matching http://arraylist.blogspot.com
  • 8. Regular expressions Quantifiers – tells the regex , how many times a pattern should be matched. “+” match minimum once or as many times as it occurs Example /go+d/ matches good but not god “*” matches preceding character 0 or more times Example /hik*e/ matches hike, hie – matches k 0 or more times between hi and e “?” matches preceding character 0 or 1 times but not more. Example /h?ello/ matches hello or ello but not hhello http://arraylist.blogspot.com
  • 9. Regular expressions {} matched characters specified number of times /a{5,10}/ - matches the character a at least 5 times , but no more than 10 times /a{5,}/ - matches 5 and more times. /a{0,2}/ - matches 0 or at the most 2 times. /a{5}/ - match exactly six times .* - matches anything between 2 set of characters /hello.*world/ matches “hello Joe welcome to the world” http://arraylist.blogspot.com
  • 10. Regular expressions Square brackets [] and character class [abcd] – match any of the characters a, b, c, d [a-d] – also means the same thing [ls]Aa[rs] – match uppercase A or lowercase a [0-9] – match a digit [A-Za-z]{5} - match any group of 5 alphabetic characters [^a-z] - match all capital case letters - ^ is a negation [*!@#$%&()] - match any of these characters http://arraylist.blogspot.com
  • 11. Regular expressions Special Character classes w – match a word character same as [a-Za-z] W – match non-word characters d –match a digit [0-9] D- match a non-digit s - match a whitespace character S - match a non-whitespace character Example - /d{3}/ - match 3 digits /sw+s/ - match a group of words surrounded by white space http://arraylist.blogspot.com
  • 12. Regular expressions Alternation and Anchors Alternation uses | which means “or” Eg. /tea|coffee/  check if string contains tea or coffee Grouping with alternation Eg. /(fr|bl|cl)og/  if string contains frog or blog or clog Anchors let you tell where you want to look for a character ^ - caret .eg. /^tea/ matches tea only if it occurs at the beginning of the line $ - dollar sign .eg. /sample$/ matches sample only at the end of the line. http://arraylist.blogspot.com
  • 13. Regular expressions Substitution Syntax – s///  s/searchstring/replacementstring/ Eg. $_ = “lies does not make sense” s/lies/truth/  “truth does not make sense” Instead of / you can use # as a substitution operator Example . s#lies#truth#; http://arraylist.blogspot.com