SlideShare a Scribd company logo
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

More Related Content

PPTX
Strings,patterns and regular expressions in perl
PPTX
Unit 1-scalar expressions and control structures
PPTX
Perl names values and variables
PPTX
Scalar expressions and control structures in perl
PPTX
Subroutines in perl
PPT
Bioinformatica 06-10-2011-p2 introduction
PPT
Php String And Regular Expressions
PDF
Python regular expressions
Strings,patterns and regular expressions in perl
Unit 1-scalar expressions and control structures
Perl names values and variables
Scalar expressions and control structures in perl
Subroutines in perl
Bioinformatica 06-10-2011-p2 introduction
Php String And Regular Expressions
Python regular expressions

What's hot (17)

PPT
Perl Presentation
PDF
Perl programming language
PDF
perltut
PPT
Regular Expressions grep and egrep
PPSX
Sed tips and_tricks
PDF
Maxbox starter20
ODP
Regular Expression
PDF
Tutorial on Regular Expression in Perl (perldoc Perlretut)
PDF
Lecture 23
PPT
Regular Expressions 2007
PPT
CGI With Object Oriented Perl
PPTX
Regular Expressions in PHP
PPT
P H P Part I, By Kian
PPTX
Operator precedance parsing
PPTX
C# slid
PPT
Javascript
PDF
Perl_Part1
Perl Presentation
Perl programming language
perltut
Regular Expressions grep and egrep
Sed tips and_tricks
Maxbox starter20
Regular Expression
Tutorial on Regular Expression in Perl (perldoc Perlretut)
Lecture 23
Regular Expressions 2007
CGI With Object Oriented Perl
Regular Expressions in PHP
P H P Part I, By Kian
Operator precedance parsing
C# slid
Javascript
Perl_Part1
Ad

Viewers also liked (10)

PPTX
Sca n instructorppt_chapter1_final
PPTX
Regexp master 2011
PDF
Erlang with Regexp Perl And Port
PDF
Regexp Master
PDF
Working with text, Regular expressions
PDF
El modelo económico de Maduro Fracasó
PDF
Mastering Regex in Perl
PDF
Browser Exploit Framework
PPT
Perl Basics for Pentesters Part 1
Sca n instructorppt_chapter1_final
Regexp master 2011
Erlang with Regexp Perl And Port
Regexp Master
Working with text, Regular expressions
El modelo económico de Maduro Fracasó
Mastering Regex in Perl
Browser Exploit Framework
Perl Basics for Pentesters Part 1
Ad

Similar to Regular expressions in Perl (20)

KEY
Regular Expressions 101
PPTX
Unit 1-strings,patterns and regular expressions
PPT
Regular Expressions in PHP, MySQL by programmerblog.net
PPT
Perl Intro 5 Regex Matches And Substitutions
PPT
regular-expressions lecture 28-string regular expression
PPT
regex.ppt
PPTX
Bioinformatics p2-p3-perl-regexes v2014
PDF
Basta mastering regex power
PPTX
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
PDF
Regex - Regular Expression Basics
PPT
RegEx : Expressions and Parsing Examples
PDF
An Introduction to Regular expressions
PDF
Don't Fear the Regex - CapitalCamp/GovDays 2014
PPTX
Regular expressions
PDF
Regexp
PDF
Regex startup
PPTX
Regular Expression Crash Course
PDF
2013 - Andrei Zmievski: Clínica Regex
ODP
CiNPA Security SIG - Regex Presentation
PDF
Regular expressions
Regular Expressions 101
Unit 1-strings,patterns and regular expressions
Regular Expressions in PHP, MySQL by programmerblog.net
Perl Intro 5 Regex Matches And Substitutions
regular-expressions lecture 28-string regular expression
regex.ppt
Bioinformatics p2-p3-perl-regexes v2014
Basta mastering regex power
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Regex - Regular Expression Basics
RegEx : Expressions and Parsing Examples
An Introduction to Regular expressions
Don't Fear the Regex - CapitalCamp/GovDays 2014
Regular expressions
Regexp
Regex startup
Regular Expression Crash Course
2013 - Andrei Zmievski: Clínica Regex
CiNPA Security SIG - Regex Presentation
Regular expressions

Recently uploaded (20)

PDF
Encapsulation theory and applications.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPT
Teaching material agriculture food technology
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Cloud computing and distributed systems.
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Approach and Philosophy of On baking technology
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
Encapsulation theory and applications.pdf
Electronic commerce courselecture one. Pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation_ Review paper, used for researhc scholars
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Digital-Transformation-Roadmap-for-Companies.pptx
Teaching material agriculture food technology
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Machine learning based COVID-19 study performance prediction
Cloud computing and distributed systems.
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
The AUB Centre for AI in Media Proposal.docx
Approach and Philosophy of On baking technology
Per capita expenditure prediction using model stacking based on satellite ima...
Dropbox Q2 2025 Financial Results & Investor Presentation
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
“AI and Expert System Decision Support & Business Intelligence Systems”
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
NewMind AI Weekly Chronicles - August'25 Week I

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