SlideShare a Scribd company logo
BY
SANA MATEEN
INTRODUCTION TO REGULAR EXPRESSIONS
 It is a way of defining patterns.
 A notation for describing the strings produced by regular expression.
 The first application of regular expressions in computer system was in the text
editors ed and sed in the UNIX system.
 Perl provides very powerful and dynamic string manipulation based on the usage of
regular expressions.
 Pattern Match – searching for a specified pattern within string.
 For example:
 A sequence motif,
 Accession number of a sequence,
 Parse HTML,
 Validating user input.
 Regular Expression (regex) – how to make a pattern match.
HOW REGEX WORK
Regex
code
Perl
compiler
Input data (e.g. sequence file)
outputregex engine
Simple Patterns
 Place the regex between a pair of forward slashes ( / / ).
 try:
 #!/usr/bin/perl
 while (<STDIN>) {
 if (/abc/) {
 print “>> found ‘abc’ in $_n”;
 }
 }
 Save then run the program. Type something on the terminal then press return.
Ctrl+C to exit script.
 If you type anything containing ‘abc’ the print statement is returned.
STAGES
1. The characters
 | ( ) [ { ^ $ * + ? .
are meta characters with special meanings in regular expression. To
use metacharacters in regular expression without a special meaning being
attached, it must be escaped with a backslash. ] and } are also
metacharacters in some circumstances.
2. Apart from meta characters any single character in a regular expression
/cat/ matches the string cat.
3. The meta characters ^ and $ act as anchors:
^ -- matches the start of the line
$ -- matches the end of the line.
so regex /^cat/ matches the string cat only if it appears at the start of
the line.
/cat$/ matches only at the end of the line.
/^cat$/ matches the line which contains the string cat and /^$/
matches an empty line.
4. The meta character dot (.) matches any single character except
newline, so/c.t/ matches cat,cot,cut, etc.
STAGES
5. A character class is set of characters enclosed in square brackets. Matches any
single character from those listed.
So /[aeiou]/- matches any vowel
/[0123456789]/-matches any digit
Or /[0-9]/
6. A character class of the form /[^....]/ matches any characters except those listed,
so /[^0-9]/ matches any non digit.
7. To remove the special meaning of minus to specify regular expression to match
arithmetic operators.
/[+-*/]/
8. Repetition of characters in regular expression can be specified by the
quantifiers
* -- zero or more occurrences
+ -- one or more occurrences
? – zero or more occurrences
9. Thus /[0-9]+/ matches an unsigned decimal number and /a.*b/ matches a substring
starting with ‘a’ and ending with ‘b’, with an indefinite number of other characters
in between.
FACILITIES
1. Alternations |
If RE1,RE2,RE3 are regular expressions, RE1|RE2|RE3 will match any one of the
components.
2. Grouping- ( )
Round Brackets can be used to group items.
/pitt the (elder|younger)/
3. Repetition counts
Explicit repetition counts can be added to a component of regular expression
/(wet[]){2}wet/ matches ‘ wet wet wet’
Full list of possible count modifiers are
{n} – must occur exactly n times
{n,} –must occur at least n times
{n,m}- must occur at least n times but no more than m times.
4. Regular expression
 Simple regex to check for an IP address:
 ^(?:[0-9]{1,3}.){3}[0-9]{1,3}$
FACILITIES
5. Non-greedy matching
A pattern including
.* matches the longest string it can find.
The pattern .*? Can be used when the shortest match is required.
? – shortest match
6.Short hand
This notation is given for frequent occurring character classes.
d – matches- digit
w – matches – word
s- matches- whitespace
D- matches any non digit character
Capitalization of notation reverses the sense
7. Anchors
b – word boundary
B – not a word boundary
/bJohn/ -matches both the target string John and Johnathan.
8. Back References
Round brackets define a series of partial matches that are remembered for use in subsequent processing or
in the RegEx itself.
9. The Match Operator
The match operator, m//, is used to match a string or statement to a regular expression. For example, to match
the character sequence "foo" against the scalar $bar, you might use a statement like this:
if ($bar =~ /foo/)
Note that the entire match expression.that is the expression on the left of =~ or !~ and the match operator,
returns true (in a scalar context) if the expression matches. Therefore the statement:
$true = ($foo =~ m/foo/);
BINDING OPERATOR
 Previous example matched against $_
 Want to match against a scalar variable?
 Binding Operator “=~” matches pattern on right against string on left.
 Usually add the m operator – clarity of code.
 $string =~ m/pattern/
MATCHING ONLY ONCE
 There is also a simpler version of the match operator - the ?PATTERN?
operator.
 This is basically identical to the m// operator except that it only matches once
within the string you are searching between each call to reset.
 For example, you can use this to get the first and last elements within a list:
 To remember which portion of string matched we use $1,$2,$3 etc
 #!/usr/bin/perl
 @list = qw/food foosball subeo footnote terfoot canic footbrdige/;
 foreach (@list) {
 $first = $1 if ?(foo.*)?; $last = $1 if /(foo.*)/;
 }
 print "First: $first, Last: $lastn";
 This will produce following result First: food, Last: footbrdige
s/PATTERN/REPLACEMENT/;
$string =~ s/dog/cat/;
#/user/bin/perl
$string = 'The cat sat on the mat';
$string =~ s/cat/dog/;
print "Final Result is $stringn";
This will produce following result
The dog sat on the mat
THE SUBSTITUTION OPERATOR
The substitution operator, s///, is really just an extension of the match operator that allows you to
replace the text matched with some new text. The basic form of the operator is:
The PATTERN is the regular expression for the text that we are looking for. The
REPLACEMENT is a specification for the text or regular expression that we want to use to
replace the found text with.
For example, we can replace all occurrences of .dog. with .cat. Using
Another example:
PATTERN MATCHING MODIFIERS
 m//i – Ignore case when pattern matching.
 m//g – Helps to count all occurrence of substring.
$count=0;
while($target =~ m/$substring/g) {
$count++
}
 m//m – treat a target string containing newline characters as multiple
lines.
 m//s –Treat a target string containing new line characters as single string, i.e
dot matches any character including newline.
 m//x – Ignore whitespace characters in the regular expression unless
they occur in character class.
 m//o – Compile regular expressions once only
THE TRANSLATION OPERATOR
 Translation is similar, but not identical, to the principles of substitution, but
unlike substitution, translation (or transliteration) does not use regular
expressions for its search on replacement values. The translation operators
are −
 tr/SEARCHLIST/REPLACEMENTLIST/cds
y/SEARCHLIST/REPLACEMENTLIST/cds
 The translation replaces all occurrences of the characters in SEARCHLIST
with the corresponding characters in REPLACEMENTLIST.
 For example, using the "The cat sat on the mat." string
 #/user/bin/perl
 $string = 'The cat sat on the mat';
 $string =~ tr/a/o/;
 print "$stringn";
 When above program is executed, it produces the following result −
 The cot sot on the mot.
TRANSLATION OPERATOR MODIFIERS
 Standard Perl ranges can also be used, allowing you to specify ranges of characters
either by letter or numerical value.
 To change the case of the string, you might use the following syntax in place of
the uc function.
 $string =~ tr/a-z/A-Z/;
 Following is the list of operators related to translation.
Modifier Description
c Complements SEARCHLIST
d Deletes found but unreplaced
characters
s Squashes duplicate replaced
characters.
SPLIT
 Syntax of split
 split REGEX, STRING will split the STRING at every match of the REGEX.
 split REGEX, STRING, LIMIT where LIMIT is a positive number. This will
split the STRING at every match of the REGEX, but will stop after it found LIMIT-
1 matches. So the number of elements it returns will be LIMIT or less.
 split REGEX - If STRING is not given, splitting the content of $_, the default
variable of Perl at every match of the REGEX.
 split without any parameter will split the content of $_ using /s+/ as REGEX.
 Simple cases
 split returns a list of strings:
 use Data::Dumper qw(Dumper); # used to dump out the contents of any
variable during the running of a program
 my $str = "ab cd ef gh ij";
 my @words = split / /, $str;
 print Dumper @words;
 The output is:
 $VAR1 = [ 'ab', 'cd', 'ef', 'gh', 'ij' ];
Strings,patterns and regular expressions in perl
Ad

More Related Content

What's hot (20)

Perl tutorial
Perl tutorialPerl tutorial
Perl tutorial
Manav Prasad
 
Regular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular ExpressionsRegular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular Expressions
Danny Bryant
 
Perl
PerlPerl
Perl
RaviShankar695257
 
Shell programming
Shell programmingShell programming
Shell programming
Moayad Moawiah
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
valuebound
 
Perl Scripting
Perl ScriptingPerl Scripting
Perl Scripting
Varadharajan Mukundan
 
Advanced perl finer points ,pack&amp;unpack,eval,files
Advanced perl   finer points ,pack&amp;unpack,eval,filesAdvanced perl   finer points ,pack&amp;unpack,eval,files
Advanced perl finer points ,pack&amp;unpack,eval,files
Shankar D
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
Mahantesh Devoor
 
Decision Making and Looping
Decision Making and LoopingDecision Making and Looping
Decision Making and Looping
Munazza-Mah-Jabeen
 
Introduction to Perl - Day 2
Introduction to Perl - Day 2Introduction to Perl - Day 2
Introduction to Perl - Day 2
Dave Cross
 
Conditionalstatement
ConditionalstatementConditionalstatement
Conditionalstatement
RaginiJain21
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
Lambert Lum
 
C++ STATEMENTS
C++ STATEMENTS C++ STATEMENTS
C++ STATEMENTS
Prof Ansari
 
LINUX:Control statements in shell programming
LINUX:Control statements in shell programmingLINUX:Control statements in shell programming
LINUX:Control statements in shell programming
bhatvijetha
 
Applet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java AppletsApplet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java Applets
amitksaha
 
Strings
StringsStrings
Strings
Mitali Chugh
 
358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_1-introduction-c_chapter-1358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_1-introduction-c_chapter-1
sumitbardhan
 
Programming in C Presentation upto FILE
Programming in C Presentation upto FILEProgramming in C Presentation upto FILE
Programming in C Presentation upto FILE
Dipta Saha
 
ARIES Recovery Algorithms
ARIES Recovery AlgorithmsARIES Recovery Algorithms
ARIES Recovery Algorithms
Pulasthi Lankeshwara
 
String functions in C
String functions in CString functions in C
String functions in C
baabtra.com - No. 1 supplier of quality freshers
 
Regular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular ExpressionsRegular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular Expressions
Danny Bryant
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
valuebound
 
Advanced perl finer points ,pack&amp;unpack,eval,files
Advanced perl   finer points ,pack&amp;unpack,eval,filesAdvanced perl   finer points ,pack&amp;unpack,eval,files
Advanced perl finer points ,pack&amp;unpack,eval,files
Shankar D
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
Mahantesh Devoor
 
Introduction to Perl - Day 2
Introduction to Perl - Day 2Introduction to Perl - Day 2
Introduction to Perl - Day 2
Dave Cross
 
Conditionalstatement
ConditionalstatementConditionalstatement
Conditionalstatement
RaginiJain21
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
Lambert Lum
 
LINUX:Control statements in shell programming
LINUX:Control statements in shell programmingLINUX:Control statements in shell programming
LINUX:Control statements in shell programming
bhatvijetha
 
Applet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java AppletsApplet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java Applets
amitksaha
 
358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_1-introduction-c_chapter-1358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_1-introduction-c_chapter-1
sumitbardhan
 
Programming in C Presentation upto FILE
Programming in C Presentation upto FILEProgramming in C Presentation upto FILE
Programming in C Presentation upto FILE
Dipta Saha
 

Viewers also liked (7)

Scalar expressions and control structures in perl
Scalar expressions and control structures in perlScalar expressions and control structures in perl
Scalar expressions and control structures in perl
sana mateen
 
Uses for scripting languages,web scripting in perl
Uses for scripting languages,web scripting in perlUses for scripting languages,web scripting in perl
Uses for scripting languages,web scripting in perl
sana mateen
 
Reading init param
Reading init paramReading init param
Reading init param
Nuha Noor
 
Jdbc in servlets
Jdbc in servletsJdbc in servlets
Jdbc in servlets
Nuha Noor
 
Http request and http response
Http request and http responseHttp request and http response
Http request and http response
Nuha Noor
 
Using cookies and sessions
Using cookies and sessionsUsing cookies and sessions
Using cookies and sessions
Nuha Noor
 
Jsp elements
Jsp elementsJsp elements
Jsp elements
Nuha Noor
 
Scalar expressions and control structures in perl
Scalar expressions and control structures in perlScalar expressions and control structures in perl
Scalar expressions and control structures in perl
sana mateen
 
Uses for scripting languages,web scripting in perl
Uses for scripting languages,web scripting in perlUses for scripting languages,web scripting in perl
Uses for scripting languages,web scripting in perl
sana mateen
 
Reading init param
Reading init paramReading init param
Reading init param
Nuha Noor
 
Jdbc in servlets
Jdbc in servletsJdbc in servlets
Jdbc in servlets
Nuha Noor
 
Http request and http response
Http request and http responseHttp request and http response
Http request and http response
Nuha Noor
 
Using cookies and sessions
Using cookies and sessionsUsing cookies and sessions
Using cookies and sessions
Nuha Noor
 
Jsp elements
Jsp elementsJsp elements
Jsp elements
Nuha Noor
 
Ad

Similar to Strings,patterns and regular expressions in perl (20)

Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introduction
Prof. Wim Van Criekinge
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20
Max Kleiner
 
Python regular expressions
Python regular expressionsPython regular expressions
Python regular expressions
Krishna Nanda
 
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
Shree M.L.Kakadiya MCA mahila college, Amreli
 
Unit 1-array,lists and hashes
Unit 1-array,lists and hashesUnit 1-array,lists and hashes
Unit 1-array,lists and hashes
sana mateen
 
Perl_Part4
Perl_Part4Perl_Part4
Perl_Part4
Frank Booth
 
Awk essentials
Awk essentialsAwk essentials
Awk essentials
Logan Palanisamy
 
Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptx
DurgaNayak4
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Raj Gupta
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
Sopan Shewale
 
newperl5
newperl5newperl5
newperl5
tutorialsruby
 
newperl5
newperl5newperl5
newperl5
tutorialsruby
 
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
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
Sujith Kumar
 
PERL Regular Expression
PERL Regular ExpressionPERL Regular Expression
PERL Regular Expression
Binsent Ribera
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
Muthuselvam RS
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
Ahmed Swilam
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Raghu nath
 
Introduction to regular expressions
Introduction to regular expressionsIntroduction to regular expressions
Introduction to regular expressions
Ben Brumfield
 
Python - Regular Expressions
Python - Regular ExpressionsPython - Regular Expressions
Python - Regular Expressions
Mukesh Tekwani
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introduction
Prof. Wim Van Criekinge
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20
Max Kleiner
 
Python regular expressions
Python regular expressionsPython regular expressions
Python regular expressions
Krishna Nanda
 
Unit 1-array,lists and hashes
Unit 1-array,lists and hashesUnit 1-array,lists and hashes
Unit 1-array,lists and hashes
sana mateen
 
Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptx
DurgaNayak4
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Raj Gupta
 
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
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
Sujith Kumar
 
PERL Regular Expression
PERL Regular ExpressionPERL Regular Expression
PERL Regular Expression
Binsent Ribera
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
Ahmed Swilam
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Raghu nath
 
Introduction to regular expressions
Introduction to regular expressionsIntroduction to regular expressions
Introduction to regular expressions
Ben Brumfield
 
Python - Regular Expressions
Python - Regular ExpressionsPython - Regular Expressions
Python - Regular Expressions
Mukesh Tekwani
 
Ad

More from sana mateen (20)

Files
FilesFiles
Files
sana mateen
 
PHP Variables and scopes
PHP Variables and scopesPHP Variables and scopes
PHP Variables and scopes
sana mateen
 
Php intro
Php introPhp intro
Php intro
sana mateen
 
Php and web forms
Php and web formsPhp and web forms
Php and web forms
sana mateen
 
Mail
MailMail
Mail
sana mateen
 
Files in php
Files in phpFiles in php
Files in php
sana mateen
 
File upload php
File upload phpFile upload php
File upload php
sana mateen
 
Regex posix
Regex posixRegex posix
Regex posix
sana mateen
 
Encryption in php
Encryption in phpEncryption in php
Encryption in php
sana mateen
 
Authentication methods
Authentication methodsAuthentication methods
Authentication methods
sana mateen
 
Xml schema
Xml schemaXml schema
Xml schema
sana mateen
 
Xml dtd
Xml dtdXml dtd
Xml dtd
sana mateen
 
Xml dom
Xml domXml dom
Xml dom
sana mateen
 
Xhtml
XhtmlXhtml
Xhtml
sana mateen
 
Intro xml
Intro xmlIntro xml
Intro xml
sana mateen
 
Dom parser
Dom parserDom parser
Dom parser
sana mateen
 
Unit 1-subroutines in perl
Unit 1-subroutines in perlUnit 1-subroutines in perl
Unit 1-subroutines in perl
sana mateen
 
Unit 1-uses for scripting languages,web scripting
Unit 1-uses for scripting languages,web scriptingUnit 1-uses for scripting languages,web scripting
Unit 1-uses for scripting languages,web scripting
sana mateen
 
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
sana mateen
 
Unit 1-introduction to scripts
Unit 1-introduction to scriptsUnit 1-introduction to scripts
Unit 1-introduction to scripts
sana mateen
 
PHP Variables and scopes
PHP Variables and scopesPHP Variables and scopes
PHP Variables and scopes
sana mateen
 
Php and web forms
Php and web formsPhp and web forms
Php and web forms
sana mateen
 
Encryption in php
Encryption in phpEncryption in php
Encryption in php
sana mateen
 
Authentication methods
Authentication methodsAuthentication methods
Authentication methods
sana mateen
 
Unit 1-subroutines in perl
Unit 1-subroutines in perlUnit 1-subroutines in perl
Unit 1-subroutines in perl
sana mateen
 
Unit 1-uses for scripting languages,web scripting
Unit 1-uses for scripting languages,web scriptingUnit 1-uses for scripting languages,web scripting
Unit 1-uses for scripting languages,web scripting
sana mateen
 
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
sana mateen
 
Unit 1-introduction to scripts
Unit 1-introduction to scriptsUnit 1-introduction to scripts
Unit 1-introduction to scripts
sana mateen
 

Recently uploaded (20)

sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
ajayrm685
 
Machine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATIONMachine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATION
DarrinBright1
 
Analog electronic circuits with some imp
Analog electronic circuits with some impAnalog electronic circuits with some imp
Analog electronic circuits with some imp
KarthikTG7
 
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
ijflsjournal087
 
PRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Academy - Functional Modeling In Action with PRIZ.pdfPRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Guru
 
Slide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptxSlide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptx
vvsasane
 
Interfacing PMW3901 Optical Flow Sensor with ESP32
Interfacing PMW3901 Optical Flow Sensor with ESP32Interfacing PMW3901 Optical Flow Sensor with ESP32
Interfacing PMW3901 Optical Flow Sensor with ESP32
CircuitDigest
 
Autodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User InterfaceAutodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User Interface
Atif Razi
 
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjjseninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
AjijahamadKhaji
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
Dynamics of Structures with Uncertain Properties.pptx
Dynamics of Structures with Uncertain Properties.pptxDynamics of Structures with Uncertain Properties.pptx
Dynamics of Structures with Uncertain Properties.pptx
University of Glasgow
 
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdfATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ssuserda39791
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
Generative AI & Large Language Models Agents
Generative AI & Large Language Models AgentsGenerative AI & Large Language Models Agents
Generative AI & Large Language Models Agents
aasgharbee22seecs
 
Computer Security Fundamentals Chapter 1
Computer Security Fundamentals Chapter 1Computer Security Fundamentals Chapter 1
Computer Security Fundamentals Chapter 1
remoteaimms
 
introduction technology technology tec.pptx
introduction technology technology tec.pptxintroduction technology technology tec.pptx
introduction technology technology tec.pptx
Iftikhar70
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
Evonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdfEvonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdf
szhang13
 
Artificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptxArtificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptx
rakshanatarajan005
 
最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制
最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制
最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制
Taqyea
 
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
ajayrm685
 
Machine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATIONMachine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATION
DarrinBright1
 
Analog electronic circuits with some imp
Analog electronic circuits with some impAnalog electronic circuits with some imp
Analog electronic circuits with some imp
KarthikTG7
 
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
ijflsjournal087
 
PRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Academy - Functional Modeling In Action with PRIZ.pdfPRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Academy - Functional Modeling In Action with PRIZ.pdf
PRIZ Guru
 
Slide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptxSlide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptx
vvsasane
 
Interfacing PMW3901 Optical Flow Sensor with ESP32
Interfacing PMW3901 Optical Flow Sensor with ESP32Interfacing PMW3901 Optical Flow Sensor with ESP32
Interfacing PMW3901 Optical Flow Sensor with ESP32
CircuitDigest
 
Autodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User InterfaceAutodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User Interface
Atif Razi
 
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjjseninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
AjijahamadKhaji
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
Dynamics of Structures with Uncertain Properties.pptx
Dynamics of Structures with Uncertain Properties.pptxDynamics of Structures with Uncertain Properties.pptx
Dynamics of Structures with Uncertain Properties.pptx
University of Glasgow
 
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdfATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ssuserda39791
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
Generative AI & Large Language Models Agents
Generative AI & Large Language Models AgentsGenerative AI & Large Language Models Agents
Generative AI & Large Language Models Agents
aasgharbee22seecs
 
Computer Security Fundamentals Chapter 1
Computer Security Fundamentals Chapter 1Computer Security Fundamentals Chapter 1
Computer Security Fundamentals Chapter 1
remoteaimms
 
introduction technology technology tec.pptx
introduction technology technology tec.pptxintroduction technology technology tec.pptx
introduction technology technology tec.pptx
Iftikhar70
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
Evonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdfEvonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdf
szhang13
 
Artificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptxArtificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptx
rakshanatarajan005
 
最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制
最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制
最新版加拿大魁北克大学蒙特利尔分校毕业证(UQAM毕业证书)原版定制
Taqyea
 

Strings,patterns and regular expressions in perl

  • 2. INTRODUCTION TO REGULAR EXPRESSIONS  It is a way of defining patterns.  A notation for describing the strings produced by regular expression.  The first application of regular expressions in computer system was in the text editors ed and sed in the UNIX system.  Perl provides very powerful and dynamic string manipulation based on the usage of regular expressions.  Pattern Match – searching for a specified pattern within string.  For example:  A sequence motif,  Accession number of a sequence,  Parse HTML,  Validating user input.  Regular Expression (regex) – how to make a pattern match.
  • 3. HOW REGEX WORK Regex code Perl compiler Input data (e.g. sequence file) outputregex engine
  • 4. Simple Patterns  Place the regex between a pair of forward slashes ( / / ).  try:  #!/usr/bin/perl  while (<STDIN>) {  if (/abc/) {  print “>> found ‘abc’ in $_n”;  }  }  Save then run the program. Type something on the terminal then press return. Ctrl+C to exit script.  If you type anything containing ‘abc’ the print statement is returned.
  • 5. STAGES 1. The characters | ( ) [ { ^ $ * + ? . are meta characters with special meanings in regular expression. To use metacharacters in regular expression without a special meaning being attached, it must be escaped with a backslash. ] and } are also metacharacters in some circumstances. 2. Apart from meta characters any single character in a regular expression /cat/ matches the string cat. 3. The meta characters ^ and $ act as anchors: ^ -- matches the start of the line $ -- matches the end of the line. so regex /^cat/ matches the string cat only if it appears at the start of the line. /cat$/ matches only at the end of the line. /^cat$/ matches the line which contains the string cat and /^$/ matches an empty line. 4. The meta character dot (.) matches any single character except newline, so/c.t/ matches cat,cot,cut, etc.
  • 6. STAGES 5. A character class is set of characters enclosed in square brackets. Matches any single character from those listed. So /[aeiou]/- matches any vowel /[0123456789]/-matches any digit Or /[0-9]/ 6. A character class of the form /[^....]/ matches any characters except those listed, so /[^0-9]/ matches any non digit. 7. To remove the special meaning of minus to specify regular expression to match arithmetic operators. /[+-*/]/ 8. Repetition of characters in regular expression can be specified by the quantifiers * -- zero or more occurrences + -- one or more occurrences ? – zero or more occurrences 9. Thus /[0-9]+/ matches an unsigned decimal number and /a.*b/ matches a substring starting with ‘a’ and ending with ‘b’, with an indefinite number of other characters in between.
  • 7. FACILITIES 1. Alternations | If RE1,RE2,RE3 are regular expressions, RE1|RE2|RE3 will match any one of the components. 2. Grouping- ( ) Round Brackets can be used to group items. /pitt the (elder|younger)/ 3. Repetition counts Explicit repetition counts can be added to a component of regular expression /(wet[]){2}wet/ matches ‘ wet wet wet’ Full list of possible count modifiers are {n} – must occur exactly n times {n,} –must occur at least n times {n,m}- must occur at least n times but no more than m times. 4. Regular expression  Simple regex to check for an IP address:  ^(?:[0-9]{1,3}.){3}[0-9]{1,3}$
  • 8. FACILITIES 5. Non-greedy matching A pattern including .* matches the longest string it can find. The pattern .*? Can be used when the shortest match is required. ? – shortest match 6.Short hand This notation is given for frequent occurring character classes. d – matches- digit w – matches – word s- matches- whitespace D- matches any non digit character Capitalization of notation reverses the sense 7. Anchors b – word boundary B – not a word boundary /bJohn/ -matches both the target string John and Johnathan. 8. Back References Round brackets define a series of partial matches that are remembered for use in subsequent processing or in the RegEx itself. 9. The Match Operator The match operator, m//, is used to match a string or statement to a regular expression. For example, to match the character sequence "foo" against the scalar $bar, you might use a statement like this: if ($bar =~ /foo/) Note that the entire match expression.that is the expression on the left of =~ or !~ and the match operator, returns true (in a scalar context) if the expression matches. Therefore the statement: $true = ($foo =~ m/foo/);
  • 9. BINDING OPERATOR  Previous example matched against $_  Want to match against a scalar variable?  Binding Operator “=~” matches pattern on right against string on left.  Usually add the m operator – clarity of code.  $string =~ m/pattern/
  • 10. MATCHING ONLY ONCE  There is also a simpler version of the match operator - the ?PATTERN? operator.  This is basically identical to the m// operator except that it only matches once within the string you are searching between each call to reset.  For example, you can use this to get the first and last elements within a list:  To remember which portion of string matched we use $1,$2,$3 etc  #!/usr/bin/perl  @list = qw/food foosball subeo footnote terfoot canic footbrdige/;  foreach (@list) {  $first = $1 if ?(foo.*)?; $last = $1 if /(foo.*)/;  }  print "First: $first, Last: $lastn";  This will produce following result First: food, Last: footbrdige
  • 11. s/PATTERN/REPLACEMENT/; $string =~ s/dog/cat/; #/user/bin/perl $string = 'The cat sat on the mat'; $string =~ s/cat/dog/; print "Final Result is $stringn"; This will produce following result The dog sat on the mat THE SUBSTITUTION OPERATOR The substitution operator, s///, is really just an extension of the match operator that allows you to replace the text matched with some new text. The basic form of the operator is: The PATTERN is the regular expression for the text that we are looking for. The REPLACEMENT is a specification for the text or regular expression that we want to use to replace the found text with. For example, we can replace all occurrences of .dog. with .cat. Using Another example:
  • 12. PATTERN MATCHING MODIFIERS  m//i – Ignore case when pattern matching.  m//g – Helps to count all occurrence of substring. $count=0; while($target =~ m/$substring/g) { $count++ }  m//m – treat a target string containing newline characters as multiple lines.  m//s –Treat a target string containing new line characters as single string, i.e dot matches any character including newline.  m//x – Ignore whitespace characters in the regular expression unless they occur in character class.  m//o – Compile regular expressions once only
  • 13. THE TRANSLATION OPERATOR  Translation is similar, but not identical, to the principles of substitution, but unlike substitution, translation (or transliteration) does not use regular expressions for its search on replacement values. The translation operators are −  tr/SEARCHLIST/REPLACEMENTLIST/cds y/SEARCHLIST/REPLACEMENTLIST/cds  The translation replaces all occurrences of the characters in SEARCHLIST with the corresponding characters in REPLACEMENTLIST.  For example, using the "The cat sat on the mat." string  #/user/bin/perl  $string = 'The cat sat on the mat';  $string =~ tr/a/o/;  print "$stringn";  When above program is executed, it produces the following result −  The cot sot on the mot.
  • 14. TRANSLATION OPERATOR MODIFIERS  Standard Perl ranges can also be used, allowing you to specify ranges of characters either by letter or numerical value.  To change the case of the string, you might use the following syntax in place of the uc function.  $string =~ tr/a-z/A-Z/;  Following is the list of operators related to translation. Modifier Description c Complements SEARCHLIST d Deletes found but unreplaced characters s Squashes duplicate replaced characters.
  • 15. SPLIT  Syntax of split  split REGEX, STRING will split the STRING at every match of the REGEX.  split REGEX, STRING, LIMIT where LIMIT is a positive number. This will split the STRING at every match of the REGEX, but will stop after it found LIMIT- 1 matches. So the number of elements it returns will be LIMIT or less.  split REGEX - If STRING is not given, splitting the content of $_, the default variable of Perl at every match of the REGEX.  split without any parameter will split the content of $_ using /s+/ as REGEX.  Simple cases  split returns a list of strings:  use Data::Dumper qw(Dumper); # used to dump out the contents of any variable during the running of a program  my $str = "ab cd ef gh ij";  my @words = split / /, $str;  print Dumper @words;  The output is:  $VAR1 = [ 'ab', 'cd', 'ef', 'gh', 'ij' ];
  翻译: