SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

Perl programming language
Perl programming languagePerl programming language
Perl programming language
Elie Obeid
 
Regular Expressions grep and egrep
Regular Expressions grep and egrepRegular Expressions grep and egrep
Regular Expressions grep and egrep
Tri Truong
 
Lecture 23
Lecture 23Lecture 23
Lecture 23
rhshriva
 
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
phelios
 

Was ist angesagt? (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
 

Andere mochten auch

Sca n instructorppt_chapter1_final
Sca n instructorppt_chapter1_finalSca n instructorppt_chapter1_final
Sca n instructorppt_chapter1_final
CamTESOL2015
 

Andere mochten auch (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
 

Ähnlich wie Regular expressions in Perl

PERL Regular Expression
PERL Regular ExpressionPERL Regular Expression
PERL Regular Expression
Binsent Ribera
 
Introduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_RIntroduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_R
Hellen Gakuruh
 
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
Kuyseng Chhoeun
 
Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013
Ben Brumfield
 
Javascript正则表达式
Javascript正则表达式Javascript正则表达式
Javascript正则表达式
ji guang
 

Ähnlich wie 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
 

Kürzlich hochgeladen

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Kürzlich hochgeladen (20)

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
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
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
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...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
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
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 

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