SlideShare una empresa de Scribd logo
1 de 67
Regular Expressions Satyanarayana D  < satyavvd@yahoo-inc.com>
Topics ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
What are Regular Expressions? ,[object Object],[object Object]
Why do we need? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
History Stephen Kleene A mathematician discovered ‘ regular sets ’.
History Ken Thompson 1968 -  Regular Expression Search Algorithm. Qed  ->  ed  ->  g/re/p
History Henry Spencer 1986 – Wrote a regex library in C
Regex Flavors ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Grammar of Regex *   RE  =  one or more non-empty ‘ branches ‘ separated by ‘|’ Branch  = one or more ‘ pieces ’ Piece  =  atom  followed by quantifier Quantifier  = ‘*,+,?’ or ‘ bound ’ Bound  =  atom{n}, atom{n,}, atom {m, n} Atom  = (RE) or    () or  ‘ ^,$,’ or  followed by `^.[$()|*+?{ or  any-char or ‘ bracket expression ’ Bracket Expression = is a list of characters enclosed in `[ ]'
Meta Chars? 2 + 4 Here ‘+’ has some special meaning In a normal Expression like :
Meta Chars  Quote the next metacharacter ^  Match the beginning of the line .  Match any character (except newline) = [^] $  Match the end of the line (or before newline at the end) |  Alternation ( )  Grouping [ ]  Character class { }  Match m to n times *  Match 0 or more times +  Match 1 or more times ?  Match 1 or 0 times
Non-printable Chars   tab  (HT, TAB)   newline  (LF, NL)   return  (CR)   form feed  (FF)   alarm (bell)  (BEL)   escape (think troff)  (ESC) 33  octal char  (example: ESC) 1B  hex char  (example: ESC) {263a} long hex char  (example: Unicode SMILEY) K  control char  (example: VT) {name} named Unicode character
Character Classes – [ ] ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[0-9]  Matches any one of 0,1,2,3,4,5,6,7,8,9. [aeiou]  Matches one English vowel char. [^aeiou]  Matches any non-vowel char. [a-z-]  Matches a to z and ‘-’ [a-z0-9]  Union matches a to z and 0 to 9. [a-z&&[m-z]]  Intersection matches m to z. [a-z-[m-z]  Subtraction matches a to l.
POSIX Character Classes – [: … :] [^[:digit:] ]=  = [^0-9]
Shorthand Chars   word character  [A-Za-z0-9_]   decimal digit  [0-9]   whitespace  [ ]   not a word character  [^A-Za-z0-9_]   not a decimal digit  [^0-9]   not whitespace  [^ ]
Anchors/Assertions ,[object Object],^  Match the beginning of the line $  Match the end of the line (or before newline at the end)   Matches only at the very beginning   Matches only at the very end   Matches like $ used in single-line mode    Matches when the current position is a word boundary lt;,gt;  Matches when the current position is a word boundary   Matches when the current position is not a word boundary
^Anchors ,[object Object],^  Match the beginning of the line Anchor matches  a  certain position  In the subject  string and  it won’t consume  a ny characters /^a/ String begin  with ‘a’
Anchors$ ,[object Object],$  Match the end of the line (or before newline at the end) Anchor matche s   a certain position  In the subject  string and  it won’t consume  any character s /s$/ String end with ‘s’
 Anchors ,[object Object],  Matches only at the very beginning Anchor matches  a certain position  In the subject  string and  it won’t consume  any characters ^ Vs
,  Anchors ,[object Object],  Matches only at the very end   Matches like $ used in single-line mode  Anchor matches  a certain position  In the subject  string and  it won’t consume  any characters $ Vs  ,
,  Anchors ,[object Object],  = |  =  Matches a word boundary   Matches when the current position is not a word boundary /2/ /2/ $ xl 2 twiki file  2  > /dev/null
Quantifiers ,[object Object],{m, n}  = Matches minimum of m and a max of n occurrences. *  =  {0,}   =  Matches zero or more occurrences ( any amount). +  =  {1,}   = Matches one or more occurrences.  ?  =  {0,1}  = Matches zero or one occurrence ( means optional ). Quantifiers ( repetition) :
Quantifiers ,[object Object],/{2,4}/   2010 /<.+>/   My first <strong> regex </strong> test. <strong> regex </strong> /+sion/  Expression If the entire match fails because they consumed too much, then they are forced to give up as much as needed to make the rest of regex succeed
Non Greedy Quantifiers {,}?   *? +?   ??   To make non greedy quantifiers append ‘?’ <.+?>   My first <strong> regex </strong> test. <strong> Use negated classes   <[^>]+>   My first <strong> regex </strong> test. <strong>
Grouping – ( ) ,[object Object],{2}-{2} -{2} ({2})?   Will match  01-01-10  and  01-01-2010  also.   ,[object Object]
Alternation - | ,[object Object],/( get | set )Value/   Match either  getValue  or  setValue . ,[object Object],[object Object],[object Object]
Capturing – ( ) ,[object Object],[object Object],[object Object],/(({2})-({2})-({2}({2})?))/  ( ( {2} ) - ( {2} ) - ({2} ( {2} ) ?) )  Today is ‘ 18-08-2010 ’.    -> date ->  18-08-2010   -> day->  18   -> month ->  08   -> year ->  2010   -> year -> last two digits ->  10
Non-Capturing sub patterns– (?: ) ,[object Object],{2}-{2} -{2} (?:{2})?   Will match  01-01-10  and  01-01-2010  also.
[object Object],(?P<name>pattern)  ->  Python Style, Perl 5.12 (?P=name)  ->  Back reference (?<name>pattern) or  (?’name’pattern)  ->Perl 5.10 <name> or ’name’ or  ->  Back reference {name} {-1}, {-2}  ->  Relative Back reference. (?<vowel>[ai]).<vowel>.  abr acada bra !! /(+)+{-1}/  &quot;Thus joyful  Troy Troy  maintained  the the  watch of night...” $date=&quot;18-08-2010&quot;; $date =~ s/(?<day>{2})-(?<month>{2})-(?<year>{4})/$+{year}-$+{month}-$+{day}/; Named Capture – (?<> )
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Before Evaluating Regex
Float number = integerpart.factionalpart Matching a float number Basic Principle – Split your task into sub tasks
Integerpart = + -> will match one or more digits Matching a float number
Matching a float number Literal dot =  Integerpart = + -> will match one or more digits
Matching a float number Literal dot =  Integerpart = + -> will match one or more digits Fractional part= + -> will match one or more digits
Integerpart = + Matching a float number Literal dot =  Fractional part = + Combine all of them = ++
Matching a float number /++/  -> Is generic. It won’t match  -123.45  or  +123.45
Matching a float number /++/  -> Is generic. It won’t match  -123.45  or  +123.45 /[+-]?++/  -> will match.
Matching a float number But It won’t match  - 123.45  or  + 123.45 /[+-]?++/  -> will match. /[+-]? *++/  -> will match. But It won’t match  123.  or  .45
Matching a float number /[+-]? *(?:++|+|+)/  -> will match. But It won’t match  123.  or  .45 /[+-]? * (?:   ++ | + | + ) /
Matching a float number /[+-]? *(?:++|+|+)(?:[eE]+)?/  -> will match. But It won’t match  10e2 or 101E5 / [+-]? * (?:   ++ | + | + ) (?:   [eE]+ )? /
Matching a float number /^[+-]? *(?:++|+|+)(?:[eE][+-]?+)?$/  -> will match. But It won’t match  10e-2 / ^[+-]? * (?:   ++ | + | + ) (?:   [eE][+-]?+ )? $/x
Match a float number /^ [+-]?*  # first, match an optional sign (?:  # then match integers or f.p. mantissas: ++  # mantissa of the form a.b |+  # mantissa of the form a. |+  # mantissa of the form .b |+  # integer of the form a ) (?:[eE][+-]?+)?  # finally, optionally match an exponent $/x;
Atomic Grouping – (?> ) ,[object Object],[object Object],+99  19999   1 9999  -> Add 1 to match -> 1 +  19 999  -> Add 9 to match -> 19 +  199 99  -> Add 9 to match -> 199 +  1999 9  -> Add 9 to match -> 1999 +  19999  -> Add 9 to match -> 19999 +   19999  -> Still need to match 99 + 99   1999 9   -> Give up a 9 + 99   199 99   -> Give up one more 9 +99  19999  -> Success
Atomic Grouping – (?> ) ,[object Object],[object Object],+xx  199Rs   1 99Rs  -> Add 1 to match -> 1 +  19 9Rs  -> Add 9 to match -> 19 +  199 Rs  -> Add 9 to match -> 199 +x  199 Rs  -> x not matched with R +x  19 9 Rs   -> Give up 9, still cannot match x +x   1 99 Rs   -> Give up 9, still cannot match x +x   1 99 Rs   -> Cannot give 1 due to + +xx  199Rs  -> Failure
Atomic Grouping – (?> ) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Atomic Grouping: Possessive Quantifiers:
Look Around Ahead Behind Positive Negative Positive Negative (?=...) (?!...) (?<=...) (?<!...) (?=...)  Zero-width positive lookahead assertion (?!...)  Zero-width negative lookahead assertion (?<=...)  Zero-width positive lookbehind assertion (?<!...)  Zero-width negative lookbehind assertion *Note  : Assertions can be nested. Example : /(?<=,   (?!   (?<=,)(?=) ) )/
Look Around ,[object Object],[object Object],[object Object],[object Object],“ I catch the housecat 'Tom-cat' with catnip” ,[object Object],[object Object],*Note  : look-behind expressions cannot be of variable length. means you cannot use quantifiers (?, *, +, or {1,5}) or alternation of different-length items inside them.
Conditional expressions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Match a (quoted)? string  ->  / ^(&quot;|')?[^”’]*(?(1))$ / Matches  'blah blah’ Matches  “blah blah” Matches   blah blah Won’t Match  ‘blah blah”
Conditional expression ,[object Object],[object Object],[object Object],/ (.)(?(<=AA)G|C)$ / ATGAAG TAGBBC GATGGC /usr/share/dict/words   -> / ^(.+)(.+)?(?(2)|)$ / aa baba beriberi maam vetitive
Recursive Patterns – (?) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Code Evaluation – (?{ }) ,[object Object],[object Object],$x = &quot;aaaa”; $x =~ /(a(?{print &quot;Yow&quot;;}))*aa/; produces Yow Yow Yow Yow
Pattern Code Expression – (??{ }) ,[object Object],[object Object],$length = 5; $char = 'a'; $str = 'aaaaabb'; $str =~ /(??{$char x $length})/x;  # matches, there are 5 of 'a'
Inline modifiers & Comments Matching can be modified inline by placing modifiers. (?i)  enables case-insensitive mode (?m)  enables multiline matching for ^ and $ (?s)  makes dot metacharacter match newline also (?x)  ignores literal whitespace (?U)  makes quantifiers ungreedy (lazy) by default $answers  =~ / (?i) y (?-i) (?:es)?/  -> Will match ‘y’, ’Y’, ’yes’, ’Yes’ but not ‘YES’. Comments can be inserted inline using (?#) construct. /^ (?#begin) + (?#match integer part)  (?#match dot) + (?#match fractional part) $/
Regex Testers Tools Editors Vim, TextMate, Edit Pad Pro, NoteTab, UltraEdit RegexBuddy Reggy –  http:// reggyapp.com http://rubular.com   (Ruby) RegexPal (JavaScript)  -  http://www.regexpal.com  http://www.gskinner.com/RegExr/ http://www.spaweditor.com/scripts/regex/index.php http://regex.larsolavtorvik.com/   (PHP, JavaScript) http://www.nregex.com/   ( .NET ) http://www.myregexp.com/  ( Java ) http://osteele.com/tools/reanimator   ( NFA Graphic repr. ) Expresso  -  http://www.ultrapico.com/Expresso.htm   ( .NET ) Regulator -   http://sourceforge.net/projects/regulator   ( .NET ) RegexRenamer -  http://regexrenamer.sourceforge.net/   ( .NET ) PowerGREP   http://www.powergrep.com/   Windows Grep  -  http://www.wingrep.com/
Regex Resources $perldoc perlre perlretut perlreref $man re_format “ Mastering Regular Expressions” by Jeffrey Friedl http://oreilly.com/catalog/9780596528126/ “ Regular Expressions Cookbook” by Jan Goyvaerts & Steven Levithan http://oreilly.com/catalog/9780596520694
Questions? * { } ^ ] + $ [ ( ? . ) - : #
Thank Y!ou * { } ^ ] + $ [ ( ? . ) - : #
Java Regex ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
PHP Regex ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
JavaScript Regex ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
.NET Regex ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Python Regex ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Ruby Regex ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Unicode Properties
Pattern Code Expression – (??{ }) ,[object Object],[object Object],Find Incremental numbers ? $str=&quot;abc  123 hai cde  34567  efg 1245 a132  123456789  10adf&quot;; print &quot;$1&quot; while($str=~/ (   ()   (?{$x=$2})   (   (??{++$x%10})   )*   )   /gx);'
Commify a number $no=123456789; substr($no,0,length($no)-1)=~s/(?=(?<=)(?:)+$)/,/g; print $no’ Produce  12,34,56,789
Find Incremental numbers ? $str=&quot;abc  123 hai cde  34567  efg 1245 a132  123456789  10adf&quot;; print &quot;$1&quot; while($str=~/ (   ()   (?{$x=$2})   (   (??{++$x%10})   )*   )   /gx);’ Non Capture group in a capture group won’t work : perl -e '$x=&quot;cat cat cat&quot;;$x=~/(cat(?:+))/;print &quot;:$1:&quot;;’

Más contenido relacionado

La actualidad más candente (20)

3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regex
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Python Data Structures and Algorithms.pptx
Python Data Structures and Algorithms.pptxPython Data Structures and Algorithms.pptx
Python Data Structures and Algorithms.pptx
 
Python : Regular expressions
Python : Regular expressionsPython : Regular expressions
Python : Regular expressions
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Regular expression
Regular expressionRegular expression
Regular expression
 
Python list
Python listPython list
Python list
 
Regex - Regular Expression Basics
Regex - Regular Expression BasicsRegex - Regular Expression Basics
Regex - Regular Expression Basics
 
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
 
Set methods in python
Set methods in pythonSet methods in python
Set methods in python
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
Strings in c
Strings in cStrings in c
Strings in c
 
Python- Regular expression
Python- Regular expressionPython- Regular expression
Python- Regular expression
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Collections in Java Notes
Collections in Java NotesCollections in Java Notes
Collections in Java Notes
 
Strings in python
Strings in pythonStrings in python
Strings in python
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
2 data types and operators in r
2 data types and operators in r2 data types and operators in r
2 data types and operators in r
 

Similar a Regular Expressions

Introduction to regular expressions
Introduction to regular expressionsIntroduction to regular expressions
Introduction to regular expressionsBen Brumfield
 
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
 
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
 
Python (regular expression)
Python (regular expression)Python (regular expression)
Python (regular expression)Chirag Shetty
 
Regular expressions quick reference
Regular expressions quick referenceRegular expressions quick reference
Regular expressions quick referencejvinhit
 
Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013Ben Brumfield
 
An Introduction to Regular expressions
An Introduction to Regular expressionsAn Introduction to Regular expressions
An Introduction to Regular expressionsYamagata Europe
 
Regular expressions in oracle
Regular expressions in oracleRegular expressions in oracle
Regular expressions in oracleLogan Palanisamy
 
Javascript正则表达式
Javascript正则表达式Javascript正则表达式
Javascript正则表达式ji guang
 
Introduction to Regular Expressions
Introduction to Regular ExpressionsIntroduction to Regular Expressions
Introduction to Regular ExpressionsMatt Casto
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsJames Gray
 
Regular Expression Cheat Sheet
Regular Expression Cheat SheetRegular Expression Cheat Sheet
Regular Expression Cheat SheetSydneyJohnson57
 
Looking for Patterns
Looking for PatternsLooking for Patterns
Looking for PatternsKeith Wright
 
Basta mastering regex power
Basta mastering regex powerBasta mastering regex power
Basta mastering regex powerMax Kleiner
 
Regex startup
Regex startupRegex startup
Regex startupPayPal
 

Similar a Regular Expressions (20)

Introduction to regular expressions
Introduction to regular expressionsIntroduction to regular expressions
Introduction to regular expressions
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introduction
 
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
 
Python (regular expression)
Python (regular expression)Python (regular expression)
Python (regular expression)
 
Regular expressions quick reference
Regular expressions quick referenceRegular expressions quick reference
Regular expressions quick reference
 
Ruby RegEx
Ruby RegExRuby RegEx
Ruby RegEx
 
Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
An Introduction to Regular expressions
An Introduction to Regular expressionsAn Introduction to Regular expressions
An Introduction to Regular expressions
 
Regular expressions in oracle
Regular expressions in oracleRegular expressions in oracle
Regular expressions in oracle
 
Javascript正则表达式
Javascript正则表达式Javascript正则表达式
Javascript正则表达式
 
Regex Basics
Regex BasicsRegex Basics
Regex Basics
 
Introduction to Regular Expressions
Introduction to Regular ExpressionsIntroduction to Regular Expressions
Introduction to Regular Expressions
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Regular Expression Cheat Sheet
Regular Expression Cheat SheetRegular Expression Cheat Sheet
Regular Expression Cheat Sheet
 
Les08
Les08Les08
Les08
 
Looking for Patterns
Looking for PatternsLooking for Patterns
Looking for Patterns
 
Basta mastering regex power
Basta mastering regex powerBasta mastering regex power
Basta mastering regex power
 
2013 - Andrei Zmievski: Clínica Regex
2013 - Andrei Zmievski: Clínica Regex2013 - Andrei Zmievski: Clínica Regex
2013 - Andrei Zmievski: Clínica Regex
 
Regex startup
Regex startupRegex startup
Regex startup
 

Último

Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
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
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 

Último (20)

Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
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
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 

Regular Expressions

  • 1. Regular Expressions Satyanarayana D < satyavvd@yahoo-inc.com>
  • 2.
  • 3.
  • 4.
  • 5. History Stephen Kleene A mathematician discovered ‘ regular sets ’.
  • 6. History Ken Thompson 1968 - Regular Expression Search Algorithm. Qed -> ed -> g/re/p
  • 7. History Henry Spencer 1986 – Wrote a regex library in C
  • 8.
  • 9. Grammar of Regex * RE = one or more non-empty ‘ branches ‘ separated by ‘|’ Branch = one or more ‘ pieces ’ Piece = atom followed by quantifier Quantifier = ‘*,+,?’ or ‘ bound ’ Bound = atom{n}, atom{n,}, atom {m, n} Atom = (RE) or () or ‘ ^,$,’ or followed by `^.[$()|*+?{ or any-char or ‘ bracket expression ’ Bracket Expression = is a list of characters enclosed in `[ ]'
  • 10. Meta Chars? 2 + 4 Here ‘+’ has some special meaning In a normal Expression like :
  • 11. Meta Chars Quote the next metacharacter ^ Match the beginning of the line . Match any character (except newline) = [^] $ Match the end of the line (or before newline at the end) | Alternation ( ) Grouping [ ] Character class { } Match m to n times * Match 0 or more times + Match 1 or more times ? Match 1 or 0 times
  • 12. Non-printable Chars tab (HT, TAB) newline (LF, NL) return (CR) form feed (FF) alarm (bell) (BEL) escape (think troff) (ESC) 33 octal char (example: ESC) 1B hex char (example: ESC) {263a} long hex char (example: Unicode SMILEY) K control char (example: VT) {name} named Unicode character
  • 13.
  • 14. POSIX Character Classes – [: … :] [^[:digit:] ]= = [^0-9]
  • 15. Shorthand Chars word character [A-Za-z0-9_] decimal digit [0-9] whitespace [ ] not a word character [^A-Za-z0-9_] not a decimal digit [^0-9] not whitespace [^ ]
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24. Non Greedy Quantifiers {,}? *? +? ?? To make non greedy quantifiers append ‘?’ <.+?> My first <strong> regex </strong> test. <strong> Use negated classes <[^>]+> My first <strong> regex </strong> test. <strong>
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31. Float number = integerpart.factionalpart Matching a float number Basic Principle – Split your task into sub tasks
  • 32. Integerpart = + -> will match one or more digits Matching a float number
  • 33. Matching a float number Literal dot = Integerpart = + -> will match one or more digits
  • 34. Matching a float number Literal dot = Integerpart = + -> will match one or more digits Fractional part= + -> will match one or more digits
  • 35. Integerpart = + Matching a float number Literal dot = Fractional part = + Combine all of them = ++
  • 36. Matching a float number /++/ -> Is generic. It won’t match -123.45 or +123.45
  • 37. Matching a float number /++/ -> Is generic. It won’t match -123.45 or +123.45 /[+-]?++/ -> will match.
  • 38. Matching a float number But It won’t match - 123.45 or + 123.45 /[+-]?++/ -> will match. /[+-]? *++/ -> will match. But It won’t match 123. or .45
  • 39. Matching a float number /[+-]? *(?:++|+|+)/ -> will match. But It won’t match 123. or .45 /[+-]? * (?: ++ | + | + ) /
  • 40. Matching a float number /[+-]? *(?:++|+|+)(?:[eE]+)?/ -> will match. But It won’t match 10e2 or 101E5 / [+-]? * (?: ++ | + | + ) (?: [eE]+ )? /
  • 41. Matching a float number /^[+-]? *(?:++|+|+)(?:[eE][+-]?+)?$/ -> will match. But It won’t match 10e-2 / ^[+-]? * (?: ++ | + | + ) (?: [eE][+-]?+ )? $/x
  • 42. Match a float number /^ [+-]?* # first, match an optional sign (?: # then match integers or f.p. mantissas: ++ # mantissa of the form a.b |+ # mantissa of the form a. |+ # mantissa of the form .b |+ # integer of the form a ) (?:[eE][+-]?+)? # finally, optionally match an exponent $/x;
  • 43.
  • 44.
  • 45.
  • 46. Look Around Ahead Behind Positive Negative Positive Negative (?=...) (?!...) (?<=...) (?<!...) (?=...) Zero-width positive lookahead assertion (?!...) Zero-width negative lookahead assertion (?<=...) Zero-width positive lookbehind assertion (?<!...) Zero-width negative lookbehind assertion *Note : Assertions can be nested. Example : /(?<=, (?! (?<=,)(?=) ) )/
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53. Inline modifiers & Comments Matching can be modified inline by placing modifiers. (?i) enables case-insensitive mode (?m) enables multiline matching for ^ and $ (?s) makes dot metacharacter match newline also (?x) ignores literal whitespace (?U) makes quantifiers ungreedy (lazy) by default $answers =~ / (?i) y (?-i) (?:es)?/ -> Will match ‘y’, ’Y’, ’yes’, ’Yes’ but not ‘YES’. Comments can be inserted inline using (?#) construct. /^ (?#begin) + (?#match integer part) (?#match dot) + (?#match fractional part) $/
  • 54. Regex Testers Tools Editors Vim, TextMate, Edit Pad Pro, NoteTab, UltraEdit RegexBuddy Reggy – http:// reggyapp.com http://rubular.com (Ruby) RegexPal (JavaScript) - http://www.regexpal.com http://www.gskinner.com/RegExr/ http://www.spaweditor.com/scripts/regex/index.php http://regex.larsolavtorvik.com/ (PHP, JavaScript) http://www.nregex.com/ ( .NET ) http://www.myregexp.com/ ( Java ) http://osteele.com/tools/reanimator ( NFA Graphic repr. ) Expresso - http://www.ultrapico.com/Expresso.htm ( .NET ) Regulator - http://sourceforge.net/projects/regulator ( .NET ) RegexRenamer - http://regexrenamer.sourceforge.net/ ( .NET ) PowerGREP http://www.powergrep.com/ Windows Grep - http://www.wingrep.com/
  • 55. Regex Resources $perldoc perlre perlretut perlreref $man re_format “ Mastering Regular Expressions” by Jeffrey Friedl http://oreilly.com/catalog/9780596528126/ “ Regular Expressions Cookbook” by Jan Goyvaerts & Steven Levithan http://oreilly.com/catalog/9780596520694
  • 56. Questions? * { } ^ ] + $ [ ( ? . ) - : #
  • 57. Thank Y!ou * { } ^ ] + $ [ ( ? . ) - : #
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 65.
  • 66. Commify a number $no=123456789; substr($no,0,length($no)-1)=~s/(?=(?<=)(?:)+$)/,/g; print $no’ Produce 12,34,56,789
  • 67. Find Incremental numbers ? $str=&quot;abc 123 hai cde 34567 efg 1245 a132 123456789 10adf&quot;; print &quot;$1&quot; while($str=~/ ( () (?{$x=$2}) ( (??{++$x%10}) )* ) /gx);’ Non Capture group in a capture group won’t work : perl -e '$x=&quot;cat cat cat&quot;;$x=~/(cat(?:+))/;print &quot;:$1:&quot;;’

Notas del editor

  1. LAN011213001-23445-819
  2. LAN011213001-23445-819
  3. LAN011213001-23445-819
  4. LAN011213001-23445-819
  5. LAN011213001-23445-819
  6. LAN011213001-23445-819
  7. LAN011213001-23445-819
  8. LAN011213001-23445-819
  9. LAN011213001-23445-819
  10. LAN011213001-23445-819
  11. LAN011213001-23445-819
  12. LAN011213001-23445-819
  13. LAN011213001-23445-819
  14. LAN011213001-23445-819
  15. LAN011213001-23445-819
  16. LAN011213001-23445-819
  17. LAN011213001-23445-819
  18. LAN011213001-23445-819
  19. LAN011213001-23445-819
  20. LAN011213001-23445-819
  21. LAN011213001-23445-819
  22. LAN011213001-23445-819
  23. LAN011213001-23445-819
  24. LAN011213001-23445-819
  25. LAN011213001-23445-819
  26. LAN011213001-23445-819
  27. LAN011213001-23445-819
  28. LAN011213001-23445-819
  29. LAN011213001-23445-819
  30. LAN011213001-23445-819
  31. LAN011213001-23445-819
  32. LAN011213001-23445-819
  33. LAN011213001-23445-819
  34. LAN011213001-23445-819
  35. LAN011213001-23445-819
  36. LAN011213001-23445-819
  37. LAN011213001-23445-819
  38. LAN011213001-23445-819
  39. LAN011213001-23445-819
  40. LAN011213001-23445-819
  41. LAN011213001-23445-819
  42. LAN011213001-23445-819
  43. LAN011213001-23445-819
  44. LAN011213001-23445-819
  45. LAN011213001-23445-819
  46. LAN011213001-23445-819
  47. LAN011213001-23445-819
  48. LAN011213001-23445-819
  49. LAN011213001-23445-819
  50. LAN011213001-23445-819
  51. LAN011213001-23445-819
  52. LAN011213001-23445-819
  53. LAN011213001-23445-819
  54. LAN011213001-23445-819 THAT’S why we need the version when promoting
  55. LAN011213001-23445-819 THAT’S why we need the version when promoting
  56. LAN011213001-23445-819
  57. LAN011213001-23445-819
  58. LAN011213001-23445-819
  59. LAN011213001-23445-819
  60. LAN011213001-23445-819
  61. LAN011213001-23445-819
  62. LAN011213001-23445-819
  63. LAN011213001-23445-819
  64. LAN011213001-23445-819
  65. LAN011213001-23445-819
  66. LAN011213001-23445-819
  67. LAN011213001-23445-819