SlideShare a Scribd company logo
Unit 6: Regular Expression VSRivera IBM Learning Services Worldwide Certified Manual
Objectives After completing this unit, you should understand the concepts of regular expressions and be able to use: Single-character patterns Grouping patterns Anchoring patterns Pattern precedence Matching and substitution operators
Regular Expression Used to describe a string of characters Perl’s match operator uses regular expression to search for matching text The substitute operator uses regular expressions to select the text to be replaced
Regular Expression A regular expression is made up of patterns of ordinary characters and special metacharacters Regular expression are usually shown in slashes: /pattern/ Perl’s regular expressions are similar to those of other UNIX programs
Types of Patterns Single character patterns A single alphanumeric character matches itself A single dot (.) matches any single character except a newline A list of characters in [ ] matches any single character contained – this is called a character class
Types of Patterns Multiple character patterns Anchoring patterns – match position rather than characters.
Types of Patterns To match metacharacter Example: . [ ] protect them with a backslash \. \[ \] The simplest pattern is an alphanumeric character which matches itself. /a/ matches the first “a” in a string. /S/ matches the first “S” etc.
Character Class Examples /[AEIOU]/ an uppercase English vowel /[2468]/ a single, non-zero, even digit /[0-5]/ define range using hyphen /[789-]/ hyphen first or last to match a hyphen /[a-z]/ lowercase alphabetic /[^0-9]/ initial caret specifies a negated character class /[^a-z]/ NOT a lowercase alphabetic English letter /[A-Za-z0-9]/ an alphanumeric – multiple ranges are OK
Character Class A character class matches any single character in the brackets Ascending ranges are allowed. To match a hyphen, make it the first or lass character in the brackets so that it cannot be mistaken for range, or escape it with a backslash.
Character Class To match a class to includes a right bracket, put the close bracket immediately after the open bracket: [][a-z] matches [ or ] or a letter. Or escape them with a backslash. If the first character of a class is a ^, the whole class is negated. – it matches a single character not in the class. Multiple ranges are also possible
Character Class Shortcuts Perl provides a set of shortcuts for character classes  Syntax: \d digits: [0-9] \D non-digits [^0-9] \w word characters: [A-Za-z0-9_] \W word characters: [^A-Za-z0-9_]  \s whitespace: [\t\n\r\f] \S non-whitespace: [^\t\n\r\f]
Character Class Shortcuts Shortcuts are “locale aware” if locales are available and configured Example: If (/ \d/ ) { print “found a digit!”; }
Character Class Shortcuts #!/usr/bin/perl -w print &quot;\nEnter a value\n&quot;; $_ =<> ; if (/\d/) { print &quot;Digit\n&quot;; } else { print &quot;not a digit!\n&quot;; }
Character Class Shortcuts #!/usr/bin/perl -w print &quot;\nEnter a value\n&quot;; $_ =<> ; if (/[\d_.]/) { print &quot;Digit, or underscore, or real\n&quot;; } else { print &quot;Not a digit, or underscore, or real!\n&quot;; }
#!/usr/bin/perl -w print “\nEnter a value\n&quot;; $_ =<> ; if (/[A-Za-z0-9_]/) { print &quot;Valid Characters\n&quot;;  } else { print &quot;Not a Valid Character!\n&quot;; }
Multiple Character Patterns We Usually need to match more than one character There are four ways of grouping patterns to match multiple characters Sequence Alternation Multipliers Parentheses as memory and for precedence
Sequence of Patterns A sequence of patterns, matches the sequence of characters matched by the patterns Example: If ( /112/ ) { print “Emergency number!”; } Match “1” followed by “1” followed by “2” If( /XyzzY/ ) { print ”Knows the magic word!”; } Match “X” then “y” then “z” then “z” then “Y” If ( /2\.4\.d/ ) { print “Reasonably current”; } Matches 2.4.any-single-digit
Sequence of Patterns #!/usr/bin/perl -w print &quot;Enter a value\n&quot;; $_ =<> ; if (/911/) { print &quot;Emergency Number\n&quot;;  } else { print &quot;Not an Emergency Number\n&quot;; }
Sequence of Patterns The simplest grouping pattern is a sequence of patterns The target string must match all the patterns, in the same order for the match to succeed.
Sequence of Patterns #!/usr/bin/perl -w print &quot;Enter Linux Version Number: &quot;; $_ =<> ; if (/Linux Version 2\.4\.\d/) { print &quot;Valid Linux Version\n&quot;;  } else { print &quot;Unknown Linux Version\n&quot;; }
Sequence of Patterns #!/usr/bin/perl -w print &quot;Enter a word: &quot;; $_ =<> ; if (/abc$/)  {print &quot;abc at the end of the string&quot;;  } elsif (/^abc/) {print &quot;abc at the beginning of the string\n&quot;;  } else { print &quot;No abc\n&quot;; }
Sequence of Patterns #!/usr/bin/perl -w print &quot;Enter a word: &quot;; $_ =<> ; if (/abc$/i)  {print &quot;abc at the end of the string&quot;;  } elsif (/^abc/i) {print &quot;abc at the beginning of the string\n&quot;;  } else { print &quot;No abc\n&quot;; }
Matching Alternatives Alternation Enables multiple patterns to be tested Separate the alternatives with a | Syntax: Pattern1|pattern2
Matching Alternatives #!/usr/bin/perl -w print &quot;Enter a number/letter: &quot;; $_ =<> ; if (/911|[A-Z]|\d./) { print &quot;Valid Number/Letter\n&quot;;  } else { print &quot;Not a Valid Number/Letter\n&quot;; }
Pattern Multipliers A Multiplier is a special character applied to the immediately preceding pattern A pattern an its multiplier can match variable numbers of character. Multipliers, or quantifiers, enables you to match variable length patterns
Pattern Multipliers Syntax: ?  zero or one * zero or more +  one or more {m,n} from m to n occurences {m,} m or more {,n} at most n {i} exactly i occurences
Pattern Multipliers Example: /ca?t/ # ct cat /ye*s/ # ys yes  yees yeees…. /wh+y/ /wh{5}0/ /ca{3,}r /co{1,4}w/
Pattern Multipliers /A*/ /y.{5}wdg.{2,3}s/ # the dot matches any character except new line
Midterm Quiz 1 Get ¼ sheet of yellow pad
 

More Related Content

What's hot (20)

PDF
Don't Fear the Regex - Northeast PHP 2015
Sandy Smith
 
PPTX
Regular Expressions in PHP
Andrew Kandels
 
PPT
Regular Expressions grep and egrep
Tri Truong
 
PPTX
Regex posix
sana mateen
 
PPTX
Javascript regular expression
Dhairya Joshi
 
PPTX
Regular Expressions 101 Introduction to Regular Expressions
Danny Bryant
 
PPT
Php String And Regular Expressions
mussawir20
 
PDF
Don't Fear the Regex WordCamp DC 2017
Sandy Smith
 
ODP
Looking for Patterns
Keith Wright
 
PPTX
Regular expressions
Ignaz Wanders
 
KEY
Regular Expressions 101
Raj Rajandran
 
PPT
Regular Expressions
Satya Narayana
 
PPT
Textpad and Regular Expressions
OCSI
 
ODP
Regular Expression
Lambert Lum
 
PPT
Regular expressions
Raj Gupta
 
ODP
Eloquent Ruby chapter 4 - Find The Right String with Regular Expression
Kuyseng Chhoeun
 
ODP
Regex Presentation
arnolambert
 
PDF
Lecture 23
rhshriva
 
PPTX
Regular expressions
Ивелин Кирилов
 
PPTX
Regular Expression
Mahzad Zahedi
 
Don't Fear the Regex - Northeast PHP 2015
Sandy Smith
 
Regular Expressions in PHP
Andrew Kandels
 
Regular Expressions grep and egrep
Tri Truong
 
Regex posix
sana mateen
 
Javascript regular expression
Dhairya Joshi
 
Regular Expressions 101 Introduction to Regular Expressions
Danny Bryant
 
Php String And Regular Expressions
mussawir20
 
Don't Fear the Regex WordCamp DC 2017
Sandy Smith
 
Looking for Patterns
Keith Wright
 
Regular expressions
Ignaz Wanders
 
Regular Expressions 101
Raj Rajandran
 
Regular Expressions
Satya Narayana
 
Textpad and Regular Expressions
OCSI
 
Regular Expression
Lambert Lum
 
Regular expressions
Raj Gupta
 
Eloquent Ruby chapter 4 - Find The Right String with Regular Expression
Kuyseng Chhoeun
 
Regex Presentation
arnolambert
 
Lecture 23
rhshriva
 
Regular expressions
Ивелин Кирилов
 
Regular Expression
Mahzad Zahedi
 

Viewers also liked (8)

PDF
Nth Dimenzion Corporate Profile 2009
Ramprasad Nagaraja
 
DOCX
Spl prelim lab 2011 2012
Binsent Ribera
 
PPT
Syntax analysis
Binsent Ribera
 
PDF
Different phases of a compiler
Sumit Sinha
 
PDF
Syntax analysis
Akshaya Arunan
 
PPT
Syntax analysis
Binsent Ribera
 
PPT
Syntactic Analysis
Aleli Lac
 
Nth Dimenzion Corporate Profile 2009
Ramprasad Nagaraja
 
Spl prelim lab 2011 2012
Binsent Ribera
 
Syntax analysis
Binsent Ribera
 
Different phases of a compiler
Sumit Sinha
 
Syntax analysis
Akshaya Arunan
 
Syntax analysis
Binsent Ribera
 
Syntactic Analysis
Aleli Lac
 
Ad

Similar to PERL Regular Expression (20)

PPT
Perl Presentation
Sopan Shewale
 
PDF
Working with text, Regular expressions
Krasimir Berov (Красимир Беров)
 
PDF
Tutorial on Regular Expression in Perl (perldoc Perlretut)
FrescatiStory
 
PPT
Bioinformatica 06-10-2011-p2 introduction
Prof. Wim Van Criekinge
 
PPTX
Bioinformatics p2-p3-perl-regexes v2014
Prof. Wim Van Criekinge
 
PPTX
Unit 1-strings,patterns and regular expressions
sana mateen
 
PPTX
Strings,patterns and regular expressions in perl
sana mateen
 
PDF
Perl_Part4
Frank Booth
 
PPT
regex.ppt
ansariparveen06
 
ODP
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?
daoswald
 
PPT
PERL Unit 6 regular expression
Binsent Ribera
 
PPTX
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Prof. Wim Van Criekinge
 
PPTX
Regular expressions
Eran Zimbler
 
PPT
Perl Intro 5 Regex Matches And Substitutions
Shaun Griffith
 
PDF
Regular expression for everyone
Sanjeev Kumar Jaiswal
 
PDF
perl-pocket
tutorialsruby
 
PDF
perl-pocket
tutorialsruby
 
PDF
perl-pocket
tutorialsruby
 
PDF
perl-pocket
tutorialsruby
 
PDF
Maxbox starter20
Max Kleiner
 
Perl Presentation
Sopan Shewale
 
Working with text, Regular expressions
Krasimir Berov (Красимир Беров)
 
Tutorial on Regular Expression in Perl (perldoc Perlretut)
FrescatiStory
 
Bioinformatica 06-10-2011-p2 introduction
Prof. Wim Van Criekinge
 
Bioinformatics p2-p3-perl-regexes v2014
Prof. Wim Van Criekinge
 
Unit 1-strings,patterns and regular expressions
sana mateen
 
Strings,patterns and regular expressions in perl
sana mateen
 
Perl_Part4
Frank Booth
 
regex.ppt
ansariparveen06
 
Regular Expressions: Backtracking, and The Little Engine that Could(n't)?
daoswald
 
PERL Unit 6 regular expression
Binsent Ribera
 
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Prof. Wim Van Criekinge
 
Regular expressions
Eran Zimbler
 
Perl Intro 5 Regex Matches And Substitutions
Shaun Griffith
 
Regular expression for everyone
Sanjeev Kumar Jaiswal
 
perl-pocket
tutorialsruby
 
perl-pocket
tutorialsruby
 
perl-pocket
tutorialsruby
 
perl-pocket
tutorialsruby
 
Maxbox starter20
Max Kleiner
 
Ad

Recently uploaded (20)

PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
community health nursing question paper 2.pdf
Prince kumar
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 

PERL Regular Expression

  • 1. Unit 6: Regular Expression VSRivera IBM Learning Services Worldwide Certified Manual
  • 2. Objectives After completing this unit, you should understand the concepts of regular expressions and be able to use: Single-character patterns Grouping patterns Anchoring patterns Pattern precedence Matching and substitution operators
  • 3. Regular Expression Used to describe a string of characters Perl’s match operator uses regular expression to search for matching text The substitute operator uses regular expressions to select the text to be replaced
  • 4. Regular Expression A regular expression is made up of patterns of ordinary characters and special metacharacters Regular expression are usually shown in slashes: /pattern/ Perl’s regular expressions are similar to those of other UNIX programs
  • 5. Types of Patterns Single character patterns A single alphanumeric character matches itself A single dot (.) matches any single character except a newline A list of characters in [ ] matches any single character contained – this is called a character class
  • 6. Types of Patterns Multiple character patterns Anchoring patterns – match position rather than characters.
  • 7. Types of Patterns To match metacharacter Example: . [ ] protect them with a backslash \. \[ \] The simplest pattern is an alphanumeric character which matches itself. /a/ matches the first “a” in a string. /S/ matches the first “S” etc.
  • 8. Character Class Examples /[AEIOU]/ an uppercase English vowel /[2468]/ a single, non-zero, even digit /[0-5]/ define range using hyphen /[789-]/ hyphen first or last to match a hyphen /[a-z]/ lowercase alphabetic /[^0-9]/ initial caret specifies a negated character class /[^a-z]/ NOT a lowercase alphabetic English letter /[A-Za-z0-9]/ an alphanumeric – multiple ranges are OK
  • 9. Character Class A character class matches any single character in the brackets Ascending ranges are allowed. To match a hyphen, make it the first or lass character in the brackets so that it cannot be mistaken for range, or escape it with a backslash.
  • 10. Character Class To match a class to includes a right bracket, put the close bracket immediately after the open bracket: [][a-z] matches [ or ] or a letter. Or escape them with a backslash. If the first character of a class is a ^, the whole class is negated. – it matches a single character not in the class. Multiple ranges are also possible
  • 11. Character Class Shortcuts Perl provides a set of shortcuts for character classes Syntax: \d digits: [0-9] \D non-digits [^0-9] \w word characters: [A-Za-z0-9_] \W word characters: [^A-Za-z0-9_] \s whitespace: [\t\n\r\f] \S non-whitespace: [^\t\n\r\f]
  • 12. Character Class Shortcuts Shortcuts are “locale aware” if locales are available and configured Example: If (/ \d/ ) { print “found a digit!”; }
  • 13. Character Class Shortcuts #!/usr/bin/perl -w print &quot;\nEnter a value\n&quot;; $_ =<> ; if (/\d/) { print &quot;Digit\n&quot;; } else { print &quot;not a digit!\n&quot;; }
  • 14. Character Class Shortcuts #!/usr/bin/perl -w print &quot;\nEnter a value\n&quot;; $_ =<> ; if (/[\d_.]/) { print &quot;Digit, or underscore, or real\n&quot;; } else { print &quot;Not a digit, or underscore, or real!\n&quot;; }
  • 15. #!/usr/bin/perl -w print “\nEnter a value\n&quot;; $_ =<> ; if (/[A-Za-z0-9_]/) { print &quot;Valid Characters\n&quot;; } else { print &quot;Not a Valid Character!\n&quot;; }
  • 16. Multiple Character Patterns We Usually need to match more than one character There are four ways of grouping patterns to match multiple characters Sequence Alternation Multipliers Parentheses as memory and for precedence
  • 17. Sequence of Patterns A sequence of patterns, matches the sequence of characters matched by the patterns Example: If ( /112/ ) { print “Emergency number!”; } Match “1” followed by “1” followed by “2” If( /XyzzY/ ) { print ”Knows the magic word!”; } Match “X” then “y” then “z” then “z” then “Y” If ( /2\.4\.d/ ) { print “Reasonably current”; } Matches 2.4.any-single-digit
  • 18. Sequence of Patterns #!/usr/bin/perl -w print &quot;Enter a value\n&quot;; $_ =<> ; if (/911/) { print &quot;Emergency Number\n&quot;; } else { print &quot;Not an Emergency Number\n&quot;; }
  • 19. Sequence of Patterns The simplest grouping pattern is a sequence of patterns The target string must match all the patterns, in the same order for the match to succeed.
  • 20. Sequence of Patterns #!/usr/bin/perl -w print &quot;Enter Linux Version Number: &quot;; $_ =<> ; if (/Linux Version 2\.4\.\d/) { print &quot;Valid Linux Version\n&quot;; } else { print &quot;Unknown Linux Version\n&quot;; }
  • 21. Sequence of Patterns #!/usr/bin/perl -w print &quot;Enter a word: &quot;; $_ =<> ; if (/abc$/) {print &quot;abc at the end of the string&quot;; } elsif (/^abc/) {print &quot;abc at the beginning of the string\n&quot;; } else { print &quot;No abc\n&quot;; }
  • 22. Sequence of Patterns #!/usr/bin/perl -w print &quot;Enter a word: &quot;; $_ =<> ; if (/abc$/i) {print &quot;abc at the end of the string&quot;; } elsif (/^abc/i) {print &quot;abc at the beginning of the string\n&quot;; } else { print &quot;No abc\n&quot;; }
  • 23. Matching Alternatives Alternation Enables multiple patterns to be tested Separate the alternatives with a | Syntax: Pattern1|pattern2
  • 24. Matching Alternatives #!/usr/bin/perl -w print &quot;Enter a number/letter: &quot;; $_ =<> ; if (/911|[A-Z]|\d./) { print &quot;Valid Number/Letter\n&quot;; } else { print &quot;Not a Valid Number/Letter\n&quot;; }
  • 25. Pattern Multipliers A Multiplier is a special character applied to the immediately preceding pattern A pattern an its multiplier can match variable numbers of character. Multipliers, or quantifiers, enables you to match variable length patterns
  • 26. Pattern Multipliers Syntax: ? zero or one * zero or more + one or more {m,n} from m to n occurences {m,} m or more {,n} at most n {i} exactly i occurences
  • 27. Pattern Multipliers Example: /ca?t/ # ct cat /ye*s/ # ys yes yees yeees…. /wh+y/ /wh{5}0/ /ca{3,}r /co{1,4}w/
  • 28. Pattern Multipliers /A*/ /y.{5}wdg.{2,3}s/ # the dot matches any character except new line
  • 29. Midterm Quiz 1 Get ¼ sheet of yellow pad
  • 30.