SlideShare a Scribd company logo
1 of 28
$address =~ m/(d* .*)n(.*?, ([A-Z]{2}) (d{5})-?(d{0,5})/
Introduction to Regular Expressions
• It’s all about patterns
• Character Classes match any text of a certain type
• Repetition operators specify a recurring pattern
• Search flags change how the RegEx operates
• In this presentation…
• green denotes a character class
• yellow denotes a repetition quantifier
• orange denotes a search flag or other symbol
• My examples use Perl syntax
Introduction to Regular Expressions
• Basic syntax
• All RegEx statements must begin and end with /
• /something/
• Escaping reserved characters is crucial
• /(i.e. / is invalid because ( must be closed
• However, /(i.e. / is valid for finding ‘(i.e. ’
• Reserved characters include:
• . * ? + ( ) [ ] { } /  |
• Also some characters have special meanings
based on their position in the statement
Regular Expression Matching
• Text Matching
• A RegEx can match plain text
• ex. if ($name =~ /Dan/) { print “match”; }
• But this will match Dan, Danny, Daniel, etc…
• Full Text Matching with Anchors
• Might want to match a whole line (or string)
• ex. if ($name =~ /^Dan$/) { print “match”; }
• This will only match Dan
• ^ anchors to the front of the line
• $ anchors to the end of the line
Regular Expression Matching
• Order of results
• The search will begin at the start of the string
• This can be altered, don’t ask yet
• Every character is important
• Any plain text in the expression is treated literally
• Nothing is neglected (close doesn’t count)
• / s/ is not the same as / s/
• Far easier to write than to debug!
Regular Expression Char Classes
• Allows specification of only certain allowable chars
• [dofZ] matches only the letters d, o, f, and Z
• If you have a string ‘dog’ then /[dofZ]/ would
match ‘d’ only even though ‘o’ is also in the class
• So this expression can be stated “match one of
either d, o, f, or Z.”
• [A-Za-z] matches any letter
• [a-fA-F0-9] matches any hexadecimal character
• [^*$/] matches anything BUT *, $, /, or 
• The ^ in the front of the char class specifies ‘not’
• In a char class, you only need to escape  ( ] - ^
Regular Expression Char Classes
• Special character classes match specific characters
• d matches a single digit
• w matches a word character (A-Z, a-z, _)
• b matches a word boundary /bwordb/
• s matches a whitespace character (spc, tab, newln)
• . wildcard matches everything except newlines
• Use very carefully, you could get anything!
• To match “anything but…” capitalize the char class
• i.e. D matches anything that isn’t a digit
Regular Expression Char Classes
• Character Class Examples
• $bodyPart =~ /eww/;
• Matches ear, eye, etc
• $thing = ‘1, 2, 3 strikes!’; $thing =~ /sd/;
• Matches ‘ 2’
• $thing = ‘1, 2, 3 strikes!’; $thing =~ /[sd]/;
• Matches ‘1’
• Not always useful to match single characters
• $phone =~ /ddd-ddd-dddd/;
• There’s a better way…
Regular Expression Repetition
• Repetition allows for flexibility
• Range of occurrences
• $weight =~ /d{2,3}/;
• Matches any weight from 10 to 999
• $name =~ /w{5,}/;
• Matches any name longer than 5 letters
• if ($SSN =~ /d{9}/) { print “Invalid SSN!”; }
• Matches exactly 9 digits
Regular Expression Repetition
• General Quantifiers
• Some more special characters
• $favoriteNumber =~ /d*/;
• Matches any size number or no number at all
• $firstName =~ /w+/;
• Matches one or more characters
• $middleInitial =~ /w?/;
• Matches one or zero characters
Regular Expression Repetition
• Greedy vs Nongreedy matching
• Greedy matching gets the longest results possible
• Nongreedy matching gets the shortest possible
• Let’s say $robot = ‘The12thRobotIs2ndInLine’
• $robot =~ /w*d+/; (greedy)
• Matches The12thRobotIs2
• Maximizes the length of w
• $robot =~ /w*?d+/; (nongreedy)
• Matches The12
• Minimizes the length of w
Regular Expression Repetition
• Greedy vs Nongreedy matching
• Suppose $txt = ‘something is so cool’;
• $txt =~ /something/;
• Matches ‘something’
• $txt =~ /so(mething)?/;
• Matches ‘something’ and the second ‘so’
• $txt =~ /so(mething)??/;
• Matches only ‘so’ and the second ‘so’
• Doesn’t really make sense to do this
Regular Expression Real Life Examples
• Using what you’ve learned so far, you can…
• Validate a standard 8.3 file name
• $path =~ /^w{1,8}.[A-Za-z0-9]{2,3}$/
• Account for poorly spelled user input
• $answer =~ /^ban{1,2}an{1,2}a$/
• $iansLastName =~ /^P[ae]t{1,2}ers[oe]n$/
• $iansFirstName =~ /^E?[Ii]?[aeo]?n$/
• Matches Ian, Ean, Eian, Eon, Ien, Ein
• At least everyone gets the n right…
Alternation
• Alternation allows multiple possibilities
• Let $story = ‘He went to get his mother’;
• $story =~ /^(He|She)b.*?b(his|her)b.*?
(mother|father|brother|sister|dog)/;
• Also matches ‘She punched her fat brother’
• Make sure the grouping is correct!
• $ans =~ /^(true|false)$/
• Matches only ‘true’ or ‘false’
• $ans =~ /^true|false$/ (same as /(^true|false$)/)
• Matches ‘true never’ or ‘not really false’
Grouping for Backreferences
• Backreferences
• With all these wildcards and possible matches, we
usually need to know what the expression finally
ended up matching.
• Backreferences let you see what was matched
• Can be used after the expression has evaluated or
even inside the expression itself
• Handled very differently in different languages
• Numbered from left to right, starting at 1
Grouping for Backreferences
• Perl backreferences
• Used inside the expression
• $txt =~ /b(w+)s+1b/
• Finds any duplicated word, must use 1 here
• Used after the expression
• $class =~ /(.+?)-(d+)/
• The first word between hyphens is stored in the
Perl variable $1 (not 1) and the number goes in $2
• print “I am in class $1, section $2”;
Grouping for Backreferences
• Java backreferences
• Annoying but still useful
• Pattern p = Pattern.compile(“(.+?)-(d+)”);
Matcher m = p.matcher(mySchedule);
m.find();
System.out.println(“I am in class ” + m.group(1) +
“, section ” + m.group(2));
• Ugly, but usually better than the alternative
• m.group() returns the entire string matched
Grouping for Backreferences
• Javascript backreferences
• Used inside the expression
• Not supported
• Used after the expression
• /(.+?)-(d+)/.test(class);
• alert(RegExp.$1);
• str = str.replace(/(S+)s+(S+)/, “$2 $1”);
• RegExp supports all of Perl’s special backreference
variables (wait a few slides)
Grouping for Backreferences
• PHP/Python backreferences
• Allows the use of specifically named backreferences
• Groups also maintain their numbers
• .NET backreferences
• Allows named backreferences
• If you try to access named groups by number, stuff
breaks
• Check the web for info on how to use backreferences
in these and other languages.
Grouping without Backreferences
• Sometimes you just need to make a group
• If important groups must be backreferenced, disable
backreferencing for any unimportant groups
• $sentence =~ /(?:He|She) likes (w+)./;
• I don’t care if it’s a he or she
• All I want to know is what he/she likes
• Therefore I use (?:) to forgo the backreference
• $1 will contain that thing that he/she likes
Matching Modes
• Matching has different functional modes
• Modes can be set by flags outside the expression (only
in some languages & implementations)
• $name =~ /[a-z]+/i;
• i turns off case sensitivity
• $xml =~ /title=“([w ]*)”.*keywords=“([w ]*)”/s;
• s enables . to match newlines
• $report =~ /^s*Name:[sS]*?The End.s*$/m;
• m allows newlines between ^ and $
Matching Modes
• Matching has different functional modes
• Modes can be set by flags inside the expression
(except in Javascript and Ruby)
• $password =~ /^[a-z](?i)[a-jp-xz0-9]{4,11}$/;
• If an insane web site specifies that your
password must begin with a lowercase letter
followed by 4 to 11 upper/lower alphanumeric
characters excluding k through o and y.
• $element =~ /^(?i)[A-Z](?-i)[a-z]?$/;
• (?i) makes the first letter case insensitive (if
they type o, but meant O, we still know they
mean oxygen). (?-i) makes sure the second
letter is lowercase, otherwise it’s 2 elements
Regular Expression Replacing
• Replacements simplify complex data modification
• Generally the first part of a replace command is the
regular expression and the second part is what to
replace the matched text with
• Usually a backreference variable can be used in the
replacement text to refer to a group matched in the
expression
• The RegEx engine continues searching at the point in
the string following the replacement
• Replacements use all the same syntax, but have
several unique features and are implemented very
differently in various languages.
Regular Expression Replacing
• Perl replacement syntax
• $phone =~ s/D//;
• Removes the first non-digit character in a phone #
• Note that leaving the replacement blank deletes
• $html =~ s/^(s*)/$1t/;
• Adds a tab to a line of HTML using backreferences
• $sample =~ s/[abc]/[ABC]/;
• Might not do what is expected
• The second part is NOT a regular expression, it’s a
string
Regular Expression Replacing
• Java replacement syntax (sucks)
• Pattern p = Pattern.compile(“server(d)”);
• p.matcher(netPath).replaceAll(“workstation$1”);
• Yes, you actually have to use 8 ’s to make 
• Any  in the expression needs to be doubled
• Matcher should parse replacement for $1
• This has the same effect but is slightly faster than
• netPath.replaceAll(“server(d)”,
“workstation$1”);
• No, you can’t seem to use .replace()…
Replacement Modes
• Replacements can be performed singly or globally
• The examples I have been using replace only single
occurrences of patterns
• Use the g flag to force the expression to scan the
entire string
• $phone =~ s/D//g;
• Removes all non-digits in the phone number
• $myGarage =~ s/Jeep|Cougar/Boeing/g;
• Gives me jets in exchange for cars
• Don’t use it if it’s not necessary
Combining Replace and Match Modes
• Combining modes is easy
• To combine modes, just append the flags
• $alphabet =~ /Q//gi;
• Get rid of the pesky letter Q (and q too)
• $response =~ /(?im)“([aeiou].*?)”(?-m)(.*)/;
• This example sucks. Point is you can combine
modes inside the statement, too.
References for Learning More
• Tutorials for other programming languages
• http://www.regular-expressions.info/
• In-depth syntax
• http://kobesearch.cpan.org/htdocs/perl/perlreref.html
• Code Search (ex: ‘ip address regex’)
• http://www.google.com/codesearch

More Related Content

Similar to regex.ppt

Practical approach to perl day1
Practical approach to perl day1Practical approach to perl day1
Practical approach to perl day1Rakesh Mukundan
 
Basta mastering regex power
Basta mastering regex powerBasta mastering regex power
Basta mastering regex powerMax Kleiner
 
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
 
Php Chapter 4 Training
Php Chapter 4 TrainingPhp Chapter 4 Training
Php Chapter 4 TrainingChris Chubb
 
Regular expressions in Perl
Regular expressions in PerlRegular expressions in Perl
Regular expressions in PerlGirish Manwani
 
Don't Fear the Regex - CapitalCamp/GovDays 2014
Don't Fear the Regex - CapitalCamp/GovDays 2014Don't Fear the Regex - CapitalCamp/GovDays 2014
Don't Fear the Regex - CapitalCamp/GovDays 2014Sandy Smith
 
Regexp secrets
Regexp secretsRegexp secrets
Regexp secretsHiro Asari
 
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...Andrea Telatin
 
Perl Intro 5 Regex Matches And Substitutions
Perl Intro 5 Regex Matches And SubstitutionsPerl Intro 5 Regex Matches And Substitutions
Perl Intro 5 Regex Matches And SubstitutionsShaun Griffith
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionProf. Wim Van Criekinge
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP StringsAhmed Swilam
 
Regular Expressions grep and egrep
Regular Expressions grep and egrepRegular Expressions grep and egrep
Regular Expressions grep and egrepTri Truong
 
Regular Expressions and You
Regular Expressions and YouRegular Expressions and You
Regular Expressions and YouJames Armes
 
Don't Fear the Regex - Northeast PHP 2015
Don't Fear the Regex - Northeast PHP 2015Don't Fear the Regex - Northeast PHP 2015
Don't Fear the Regex - Northeast PHP 2015Sandy Smith
 
Don't Fear the Regex LSP15
Don't Fear the Regex LSP15Don't Fear the Regex LSP15
Don't Fear the Regex LSP15Sandy Smith
 

Similar to regex.ppt (20)

Practical approach to perl day1
Practical approach to perl day1Practical approach to perl day1
Practical approach to perl day1
 
Basta mastering regex power
Basta mastering regex powerBasta mastering regex power
Basta mastering regex power
 
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)
 
Perl Introduction
Perl IntroductionPerl Introduction
Perl Introduction
 
Php Chapter 4 Training
Php Chapter 4 TrainingPhp Chapter 4 Training
Php Chapter 4 Training
 
Regular expressions in Perl
Regular expressions in PerlRegular expressions in Perl
Regular expressions in Perl
 
Don't Fear the Regex - CapitalCamp/GovDays 2014
Don't Fear the Regex - CapitalCamp/GovDays 2014Don't Fear the Regex - CapitalCamp/GovDays 2014
Don't Fear the Regex - CapitalCamp/GovDays 2014
 
Regexp secrets
Regexp secretsRegexp secrets
Regexp secrets
 
Ruby_Basic
Ruby_BasicRuby_Basic
Ruby_Basic
 
Perl_Tutorial_v1
Perl_Tutorial_v1Perl_Tutorial_v1
Perl_Tutorial_v1
 
Perl_Tutorial_v1
Perl_Tutorial_v1Perl_Tutorial_v1
Perl_Tutorial_v1
 
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
 
Perl Intro 5 Regex Matches And Substitutions
Perl Intro 5 Regex Matches And SubstitutionsPerl Intro 5 Regex Matches And Substitutions
Perl Intro 5 Regex Matches And Substitutions
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introduction
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
 
Regular Expressions grep and egrep
Regular Expressions grep and egrepRegular Expressions grep and egrep
Regular Expressions grep and egrep
 
Regular Expressions and You
Regular Expressions and YouRegular Expressions and You
Regular Expressions and You
 
Don't Fear the Regex - Northeast PHP 2015
Don't Fear the Regex - Northeast PHP 2015Don't Fear the Regex - Northeast PHP 2015
Don't Fear the Regex - Northeast PHP 2015
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Don't Fear the Regex LSP15
Don't Fear the Regex LSP15Don't Fear the Regex LSP15
Don't Fear the Regex LSP15
 

More from ansariparveen06

discrete mathematics binary%20trees.pptx
discrete mathematics binary%20trees.pptxdiscrete mathematics binary%20trees.pptx
discrete mathematics binary%20trees.pptxansariparveen06
 
Introduction to Arduino 16822775 (2).ppt
Introduction to Arduino 16822775 (2).pptIntroduction to Arduino 16822775 (2).ppt
Introduction to Arduino 16822775 (2).pptansariparveen06
 
Fundamentals of programming Arduino-Wk2.ppt
Fundamentals of programming Arduino-Wk2.pptFundamentals of programming Arduino-Wk2.ppt
Fundamentals of programming Arduino-Wk2.pptansariparveen06
 
Combinational_Logic_Circuit.pptx
Combinational_Logic_Circuit.pptxCombinational_Logic_Circuit.pptx
Combinational_Logic_Circuit.pptxansariparveen06
 
presentation_python_7_1569170870_375360.pptx
presentation_python_7_1569170870_375360.pptxpresentation_python_7_1569170870_375360.pptx
presentation_python_7_1569170870_375360.pptxansariparveen06
 
BCom-Sem2-Marketing-Digital-payment-Presentation.pptx
BCom-Sem2-Marketing-Digital-payment-Presentation.pptxBCom-Sem2-Marketing-Digital-payment-Presentation.pptx
BCom-Sem2-Marketing-Digital-payment-Presentation.pptxansariparveen06
 
May14ProcessScheduling.ppt
May14ProcessScheduling.pptMay14ProcessScheduling.ppt
May14ProcessScheduling.pptansariparveen06
 
UNIPROCESS SCHEDULING.pptx
UNIPROCESS SCHEDULING.pptxUNIPROCESS SCHEDULING.pptx
UNIPROCESS SCHEDULING.pptxansariparveen06
 
1-introduction-to-dart-programming.pptx
1-introduction-to-dart-programming.pptx1-introduction-to-dart-programming.pptx
1-introduction-to-dart-programming.pptxansariparveen06
 
exception%20handlingcpp.pptx
exception%20handlingcpp.pptxexception%20handlingcpp.pptx
exception%20handlingcpp.pptxansariparveen06
 

More from ansariparveen06 (20)

discrete mathematics binary%20trees.pptx
discrete mathematics binary%20trees.pptxdiscrete mathematics binary%20trees.pptx
discrete mathematics binary%20trees.pptx
 
Introduction to Arduino 16822775 (2).ppt
Introduction to Arduino 16822775 (2).pptIntroduction to Arduino 16822775 (2).ppt
Introduction to Arduino 16822775 (2).ppt
 
Fundamentals of programming Arduino-Wk2.ppt
Fundamentals of programming Arduino-Wk2.pptFundamentals of programming Arduino-Wk2.ppt
Fundamentals of programming Arduino-Wk2.ppt
 
pscheduling.ppt
pscheduling.pptpscheduling.ppt
pscheduling.ppt
 
kmap.pptx
kmap.pptxkmap.pptx
kmap.pptx
 
Combinational_Logic_Circuit.pptx
Combinational_Logic_Circuit.pptxCombinational_Logic_Circuit.pptx
Combinational_Logic_Circuit.pptx
 
presentation_python_7_1569170870_375360.pptx
presentation_python_7_1569170870_375360.pptxpresentation_python_7_1569170870_375360.pptx
presentation_python_7_1569170870_375360.pptx
 
BCom-Sem2-Marketing-Digital-payment-Presentation.pptx
BCom-Sem2-Marketing-Digital-payment-Presentation.pptxBCom-Sem2-Marketing-Digital-payment-Presentation.pptx
BCom-Sem2-Marketing-Digital-payment-Presentation.pptx
 
dsa.ppt
dsa.pptdsa.ppt
dsa.ppt
 
11-IOManagement.ppt
11-IOManagement.ppt11-IOManagement.ppt
11-IOManagement.ppt
 
May14ProcessScheduling.ppt
May14ProcessScheduling.pptMay14ProcessScheduling.ppt
May14ProcessScheduling.ppt
 
UNIPROCESS SCHEDULING.pptx
UNIPROCESS SCHEDULING.pptxUNIPROCESS SCHEDULING.pptx
UNIPROCESS SCHEDULING.pptx
 
1-introduction-to-dart-programming.pptx
1-introduction-to-dart-programming.pptx1-introduction-to-dart-programming.pptx
1-introduction-to-dart-programming.pptx
 
CHAP4.pptx
CHAP4.pptxCHAP4.pptx
CHAP4.pptx
 
green IT cooling.pptx
green IT cooling.pptxgreen IT cooling.pptx
green IT cooling.pptx
 
06-Deadlocks.ppt
06-Deadlocks.ppt06-Deadlocks.ppt
06-Deadlocks.ppt
 
chp9 green IT.pptx
chp9 green IT.pptxchp9 green IT.pptx
chp9 green IT.pptx
 
BOM.ppt
BOM.pptBOM.ppt
BOM.ppt
 
Cooling.pptx
Cooling.pptxCooling.pptx
Cooling.pptx
 
exception%20handlingcpp.pptx
exception%20handlingcpp.pptxexception%20handlingcpp.pptx
exception%20handlingcpp.pptx
 

Recently uploaded

MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 

regex.ppt

  • 1. $address =~ m/(d* .*)n(.*?, ([A-Z]{2}) (d{5})-?(d{0,5})/
  • 2. Introduction to Regular Expressions • It’s all about patterns • Character Classes match any text of a certain type • Repetition operators specify a recurring pattern • Search flags change how the RegEx operates • In this presentation… • green denotes a character class • yellow denotes a repetition quantifier • orange denotes a search flag or other symbol • My examples use Perl syntax
  • 3. Introduction to Regular Expressions • Basic syntax • All RegEx statements must begin and end with / • /something/ • Escaping reserved characters is crucial • /(i.e. / is invalid because ( must be closed • However, /(i.e. / is valid for finding ‘(i.e. ’ • Reserved characters include: • . * ? + ( ) [ ] { } / | • Also some characters have special meanings based on their position in the statement
  • 4. Regular Expression Matching • Text Matching • A RegEx can match plain text • ex. if ($name =~ /Dan/) { print “match”; } • But this will match Dan, Danny, Daniel, etc… • Full Text Matching with Anchors • Might want to match a whole line (or string) • ex. if ($name =~ /^Dan$/) { print “match”; } • This will only match Dan • ^ anchors to the front of the line • $ anchors to the end of the line
  • 5. Regular Expression Matching • Order of results • The search will begin at the start of the string • This can be altered, don’t ask yet • Every character is important • Any plain text in the expression is treated literally • Nothing is neglected (close doesn’t count) • / s/ is not the same as / s/ • Far easier to write than to debug!
  • 6. Regular Expression Char Classes • Allows specification of only certain allowable chars • [dofZ] matches only the letters d, o, f, and Z • If you have a string ‘dog’ then /[dofZ]/ would match ‘d’ only even though ‘o’ is also in the class • So this expression can be stated “match one of either d, o, f, or Z.” • [A-Za-z] matches any letter • [a-fA-F0-9] matches any hexadecimal character • [^*$/] matches anything BUT *, $, /, or • The ^ in the front of the char class specifies ‘not’ • In a char class, you only need to escape ( ] - ^
  • 7. Regular Expression Char Classes • Special character classes match specific characters • d matches a single digit • w matches a word character (A-Z, a-z, _) • b matches a word boundary /bwordb/ • s matches a whitespace character (spc, tab, newln) • . wildcard matches everything except newlines • Use very carefully, you could get anything! • To match “anything but…” capitalize the char class • i.e. D matches anything that isn’t a digit
  • 8. Regular Expression Char Classes • Character Class Examples • $bodyPart =~ /eww/; • Matches ear, eye, etc • $thing = ‘1, 2, 3 strikes!’; $thing =~ /sd/; • Matches ‘ 2’ • $thing = ‘1, 2, 3 strikes!’; $thing =~ /[sd]/; • Matches ‘1’ • Not always useful to match single characters • $phone =~ /ddd-ddd-dddd/; • There’s a better way…
  • 9. Regular Expression Repetition • Repetition allows for flexibility • Range of occurrences • $weight =~ /d{2,3}/; • Matches any weight from 10 to 999 • $name =~ /w{5,}/; • Matches any name longer than 5 letters • if ($SSN =~ /d{9}/) { print “Invalid SSN!”; } • Matches exactly 9 digits
  • 10. Regular Expression Repetition • General Quantifiers • Some more special characters • $favoriteNumber =~ /d*/; • Matches any size number or no number at all • $firstName =~ /w+/; • Matches one or more characters • $middleInitial =~ /w?/; • Matches one or zero characters
  • 11. Regular Expression Repetition • Greedy vs Nongreedy matching • Greedy matching gets the longest results possible • Nongreedy matching gets the shortest possible • Let’s say $robot = ‘The12thRobotIs2ndInLine’ • $robot =~ /w*d+/; (greedy) • Matches The12thRobotIs2 • Maximizes the length of w • $robot =~ /w*?d+/; (nongreedy) • Matches The12 • Minimizes the length of w
  • 12. Regular Expression Repetition • Greedy vs Nongreedy matching • Suppose $txt = ‘something is so cool’; • $txt =~ /something/; • Matches ‘something’ • $txt =~ /so(mething)?/; • Matches ‘something’ and the second ‘so’ • $txt =~ /so(mething)??/; • Matches only ‘so’ and the second ‘so’ • Doesn’t really make sense to do this
  • 13. Regular Expression Real Life Examples • Using what you’ve learned so far, you can… • Validate a standard 8.3 file name • $path =~ /^w{1,8}.[A-Za-z0-9]{2,3}$/ • Account for poorly spelled user input • $answer =~ /^ban{1,2}an{1,2}a$/ • $iansLastName =~ /^P[ae]t{1,2}ers[oe]n$/ • $iansFirstName =~ /^E?[Ii]?[aeo]?n$/ • Matches Ian, Ean, Eian, Eon, Ien, Ein • At least everyone gets the n right…
  • 14. Alternation • Alternation allows multiple possibilities • Let $story = ‘He went to get his mother’; • $story =~ /^(He|She)b.*?b(his|her)b.*? (mother|father|brother|sister|dog)/; • Also matches ‘She punched her fat brother’ • Make sure the grouping is correct! • $ans =~ /^(true|false)$/ • Matches only ‘true’ or ‘false’ • $ans =~ /^true|false$/ (same as /(^true|false$)/) • Matches ‘true never’ or ‘not really false’
  • 15. Grouping for Backreferences • Backreferences • With all these wildcards and possible matches, we usually need to know what the expression finally ended up matching. • Backreferences let you see what was matched • Can be used after the expression has evaluated or even inside the expression itself • Handled very differently in different languages • Numbered from left to right, starting at 1
  • 16. Grouping for Backreferences • Perl backreferences • Used inside the expression • $txt =~ /b(w+)s+1b/ • Finds any duplicated word, must use 1 here • Used after the expression • $class =~ /(.+?)-(d+)/ • The first word between hyphens is stored in the Perl variable $1 (not 1) and the number goes in $2 • print “I am in class $1, section $2”;
  • 17. Grouping for Backreferences • Java backreferences • Annoying but still useful • Pattern p = Pattern.compile(“(.+?)-(d+)”); Matcher m = p.matcher(mySchedule); m.find(); System.out.println(“I am in class ” + m.group(1) + “, section ” + m.group(2)); • Ugly, but usually better than the alternative • m.group() returns the entire string matched
  • 18. Grouping for Backreferences • Javascript backreferences • Used inside the expression • Not supported • Used after the expression • /(.+?)-(d+)/.test(class); • alert(RegExp.$1); • str = str.replace(/(S+)s+(S+)/, “$2 $1”); • RegExp supports all of Perl’s special backreference variables (wait a few slides)
  • 19. Grouping for Backreferences • PHP/Python backreferences • Allows the use of specifically named backreferences • Groups also maintain their numbers • .NET backreferences • Allows named backreferences • If you try to access named groups by number, stuff breaks • Check the web for info on how to use backreferences in these and other languages.
  • 20. Grouping without Backreferences • Sometimes you just need to make a group • If important groups must be backreferenced, disable backreferencing for any unimportant groups • $sentence =~ /(?:He|She) likes (w+)./; • I don’t care if it’s a he or she • All I want to know is what he/she likes • Therefore I use (?:) to forgo the backreference • $1 will contain that thing that he/she likes
  • 21. Matching Modes • Matching has different functional modes • Modes can be set by flags outside the expression (only in some languages & implementations) • $name =~ /[a-z]+/i; • i turns off case sensitivity • $xml =~ /title=“([w ]*)”.*keywords=“([w ]*)”/s; • s enables . to match newlines • $report =~ /^s*Name:[sS]*?The End.s*$/m; • m allows newlines between ^ and $
  • 22. Matching Modes • Matching has different functional modes • Modes can be set by flags inside the expression (except in Javascript and Ruby) • $password =~ /^[a-z](?i)[a-jp-xz0-9]{4,11}$/; • If an insane web site specifies that your password must begin with a lowercase letter followed by 4 to 11 upper/lower alphanumeric characters excluding k through o and y. • $element =~ /^(?i)[A-Z](?-i)[a-z]?$/; • (?i) makes the first letter case insensitive (if they type o, but meant O, we still know they mean oxygen). (?-i) makes sure the second letter is lowercase, otherwise it’s 2 elements
  • 23. Regular Expression Replacing • Replacements simplify complex data modification • Generally the first part of a replace command is the regular expression and the second part is what to replace the matched text with • Usually a backreference variable can be used in the replacement text to refer to a group matched in the expression • The RegEx engine continues searching at the point in the string following the replacement • Replacements use all the same syntax, but have several unique features and are implemented very differently in various languages.
  • 24. Regular Expression Replacing • Perl replacement syntax • $phone =~ s/D//; • Removes the first non-digit character in a phone # • Note that leaving the replacement blank deletes • $html =~ s/^(s*)/$1t/; • Adds a tab to a line of HTML using backreferences • $sample =~ s/[abc]/[ABC]/; • Might not do what is expected • The second part is NOT a regular expression, it’s a string
  • 25. Regular Expression Replacing • Java replacement syntax (sucks) • Pattern p = Pattern.compile(“server(d)”); • p.matcher(netPath).replaceAll(“workstation$1”); • Yes, you actually have to use 8 ’s to make • Any in the expression needs to be doubled • Matcher should parse replacement for $1 • This has the same effect but is slightly faster than • netPath.replaceAll(“server(d)”, “workstation$1”); • No, you can’t seem to use .replace()…
  • 26. Replacement Modes • Replacements can be performed singly or globally • The examples I have been using replace only single occurrences of patterns • Use the g flag to force the expression to scan the entire string • $phone =~ s/D//g; • Removes all non-digits in the phone number • $myGarage =~ s/Jeep|Cougar/Boeing/g; • Gives me jets in exchange for cars • Don’t use it if it’s not necessary
  • 27. Combining Replace and Match Modes • Combining modes is easy • To combine modes, just append the flags • $alphabet =~ /Q//gi; • Get rid of the pesky letter Q (and q too) • $response =~ /(?im)“([aeiou].*?)”(?-m)(.*)/; • This example sucks. Point is you can combine modes inside the statement, too.
  • 28. References for Learning More • Tutorials for other programming languages • http://www.regular-expressions.info/ • In-depth syntax • http://kobesearch.cpan.org/htdocs/perl/perlreref.html • Code Search (ex: ‘ip address regex’) • http://www.google.com/codesearch