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

More Related Content

What's hot

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
 

What's hot (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
 

Viewers also liked (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
 

Similar to 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
 

Similar to 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
 

Recently uploaded

MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMKumar Satyam
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAnitaRaj43
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringWSO2
 
Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseWSO2
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)Samir Dash
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....rightmanforbloodline
 
API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governanceWSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 

Recently uploaded (20)

MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software Engineering
 
Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern Enterprise
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
 
API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governance
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 

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