SlideShare uma empresa Scribd logo
1 de 14
Perl Brown Bag


           Regular Expressions
                Matching
              Substitution
                Shaun Griffith
                April 10, 2006

05/13/12          ATI Confidential   1
Agenda

•Regular Expressions
•Matches
•Substitutions




05/13/12                  2
Regular Expressions

Computer Science gives us the term Regular
Expression, the topic concerned with deciding if a given
string matches a pattern.
“Regular” refers to a type of formal language. It turns out
that Perl’s regular expressions aren’t regular.
What does that mean? Perl is more powerful than
regular expressions. (But we still call them regexes.)
Regular expressions are used in compilers, parsers,
password checkers, web form checkers, and of course
datalog yield reporters.
05/13/12                                                   3
Meta Characters
Metacharacters are special characters
          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




05/13/12                                          4
Quantifiers
Quantifiers: how many?
    *       Match 0 or more times
    +       Match 1 or more times
    ?       Match 1 or 0 times
    {n}     Match exactly n times
    {n,}    Match at least n times
    {n,m}   Match between n and m times
             ( n,m <=32766)




05/13/12                                  5
Special Characters
Stuff That’s Hard to Type
    t     tab
    n     newline
    r      return (carriage
    return)
    f     form feed
    e     escape
    033   octal char
    x1B   hex char
    c[    control char




05/13/12                              6
Character Classes
Regex Pattern Shortcuts
    w     Match a "word" character
            (alphanumeric plus "_")
    W     Match a non-"word" character
    s     Match a whitespace character
    S     Match a non-whitespace character
    d     Match a digit character
    D     Match a non-digit character




05/13/12                                      7
Character Classes…
More on Character Classes:
    ^      As the first char, negates the class
           ( [^A] matches anything except “A” )
           Otherwise literal caret.
    -      between 2 chars is a range
             ( [A-G] will match A,B,C,D,E,F, or G )
           otherwise literal dash
    b     backspace (easiest way to get one)
    ]      as first char, literal close bracket


wWsSdD can all be used in a character class
(but not in a range)

05/13/12                                              8
Zero Width Characters
These don’t take up any space:
    b     Match a word boundary
    B     Match a non-(word boundary)
    A     Match only at beginning of string
    Z     Match only at end of string,
            or before newline at the end
    z     Match only at end of string




05/13/12                                       9
Regex Engine Modifiers
Change how the pattern works:
    /i     ignore case
    /g     return all matches
    /s     period (.) now matches newline (n)
    /m     “multi-line”: ^ and $ match near
            any n, not just start/end of string
    /x     “expanded” regex: space and newlines
            are ignored, and # comments allowed:
            Example:
              /w+        # first name
               s+        # space
               d+        # age
              /x
05/13/12                                           10
Matching and Capturing
Basic Matching:
     $var =~ m/something here/;
Capturing:
     $var =~ m/(pattern)/;
$1 contains “pattern”
     $catch = $1;
or
     ($catch) = $var =~ m/(pattern)/;
     ($x,$y) = $var =~ m/(first) (second)/;
Backreferences: refer to previous match
     $var = “The red red book.”;
     $var =~ m/(red) 1/;


05/13/12                                      11
Substitutions
Basic Substitutions:
    $var =~ s/pattern/replacement/;
“this” is any m/pattern/
“that” is a double-quoted string
For example
    $var =~ s/(first) (last)/$2 $1/;
Backreferences belong in pattern only!
Inline captures don’t work with s///:
    ($x) = $var =~ s/(this)/that/;       # wrong
Use $1 instead:
    $x = $1;




05/13/12                                           12
Putting It All Together
Wacky Test Names
    while (<>)
    { #              FREQ   BYP4       TRY
        if ( m/mpll_(d+)_(byp|by4)_(w)/i )
        {   $data{$die}{FREQ} = $1;
            $data{$die}{BYP4} = $2;
            $data{$die}{TRY}   = $3;
            next;
        }
    }




05/13/12                                       13
Next Time?
FTP?
     •Using Net::FTP
     •Navigating Remotely
     •Moving Files
Subroutines?
     •Passing parameters
     •Catching parameters
     •Returning data
Filehandles?
     •Open
     •Close
     •EOF


05/13/12                                 14

Mais conteúdo relacionado

Mais procurados

Regex Presentation
Regex PresentationRegex Presentation
Regex Presentation
arnolambert
 
Regular Expressions grep and egrep
Regular Expressions grep and egrepRegular Expressions grep and egrep
Regular Expressions grep and egrep
Tri Truong
 
Ch.11.1 Adding and Subtracting Polynomials
Ch.11.1 Adding and Subtracting PolynomialsCh.11.1 Adding and Subtracting Polynomials
Ch.11.1 Adding and Subtracting Polynomials
mdicken
 
Lecture 23
Lecture 23Lecture 23
Lecture 23
rhshriva
 
The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++
Anjesh Tuladhar
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
Bharat17485
 
Ch08 - Manipulating Data in Strings and Arrays
Ch08 - Manipulating Data in Strings and ArraysCh08 - Manipulating Data in Strings and Arrays
Ch08 - Manipulating Data in Strings and Arrays
dcomfort6819
 
16 Java Regex
16 Java Regex16 Java Regex
16 Java Regex
wayn
 

Mais procurados (20)

Regex Presentation
Regex PresentationRegex Presentation
Regex Presentation
 
Regular expression
Regular expressionRegular expression
Regular expression
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
 
Regular Expressions grep and egrep
Regular Expressions grep and egrepRegular Expressions grep and egrep
Regular Expressions grep and egrep
 
Introduction to Regular Expressions
Introduction to Regular ExpressionsIntroduction to Regular Expressions
Introduction to Regular Expressions
 
Ch.11.1 Adding and Subtracting Polynomials
Ch.11.1 Adding and Subtracting PolynomialsCh.11.1 Adding and Subtracting Polynomials
Ch.11.1 Adding and Subtracting Polynomials
 
Lecture 23
Lecture 23Lecture 23
Lecture 23
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++
 
Regular expressions quick reference
Regular expressions quick referenceRegular expressions quick reference
Regular expressions quick reference
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
 
Bioinformatica p2-p3-introduction
Bioinformatica p2-p3-introductionBioinformatica p2-p3-introduction
Bioinformatica p2-p3-introduction
 
Ch08 - Manipulating Data in Strings and Arrays
Ch08 - Manipulating Data in Strings and ArraysCh08 - Manipulating Data in Strings and Arrays
Ch08 - Manipulating Data in Strings and Arrays
 
Regular expression examples
Regular expression examplesRegular expression examples
Regular expression examples
 
16 Java Regex
16 Java Regex16 Java Regex
16 Java Regex
 
Finaal application on regular expression
Finaal application on regular expressionFinaal application on regular expression
Finaal application on regular expression
 
Perl names values and variables
Perl names values and variablesPerl names values and variables
Perl names values and variables
 
Introduction to Regular Expressions
Introduction to Regular ExpressionsIntroduction to Regular Expressions
Introduction to Regular Expressions
 
Perl File Handling and Regex
Perl File Handling and RegexPerl File Handling and Regex
Perl File Handling and Regex
 

Semelhante a Perl Intro 5 Regex Matches And Substitutions

Perl Intro 3 Datalog Parsing
Perl Intro 3 Datalog ParsingPerl Intro 3 Datalog Parsing
Perl Intro 3 Datalog Parsing
Shaun Griffith
 
Eloquent Ruby chapter 4 - Find The Right String with Regular Expression
Eloquent Ruby chapter 4 - Find The Right String with Regular ExpressionEloquent Ruby chapter 4 - Find The Right String with Regular Expression
Eloquent Ruby chapter 4 - Find The Right String with Regular Expression
Kuyseng Chhoeun
 
PERL Regular Expression
PERL Regular ExpressionPERL Regular Expression
PERL Regular Expression
Binsent Ribera
 
Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013
Ben Brumfield
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
Sway Wang
 
Perl Intro 7 Subroutines
Perl Intro 7 SubroutinesPerl Intro 7 Subroutines
Perl Intro 7 Subroutines
Shaun Griffith
 
Perl Intro 2 First Program
Perl Intro 2 First ProgramPerl Intro 2 First Program
Perl Intro 2 First Program
Shaun Griffith
 

Semelhante a Perl Intro 5 Regex Matches And Substitutions (20)

Php Chapter 4 Training
Php Chapter 4 TrainingPhp Chapter 4 Training
Php Chapter 4 Training
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introduction
 
Perl Intro 3 Datalog Parsing
Perl Intro 3 Datalog ParsingPerl Intro 3 Datalog Parsing
Perl Intro 3 Datalog Parsing
 
regex.ppt
regex.pptregex.ppt
regex.ppt
 
Eloquent Ruby chapter 4 - Find The Right String with Regular Expression
Eloquent Ruby chapter 4 - Find The Right String with Regular ExpressionEloquent Ruby chapter 4 - Find The Right String with Regular Expression
Eloquent Ruby chapter 4 - Find The Right String with Regular Expression
 
Introduction to regular expressions
Introduction to regular expressionsIntroduction to regular expressions
Introduction to regular expressions
 
PERL Regular Expression
PERL Regular ExpressionPERL Regular Expression
PERL Regular Expression
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
And now you have two problems. Ruby regular expressions for fun and profit by...
And now you have two problems. Ruby regular expressions for fun and profit by...And now you have two problems. Ruby regular expressions for fun and profit by...
And now you have two problems. Ruby regular expressions for fun and profit by...
 
Working with text, Regular expressions
Working with text, Regular expressionsWorking with text, Regular expressions
Working with text, Regular expressions
 
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 in Stata
Regular Expressions in StataRegular Expressions in Stata
Regular Expressions in Stata
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Basta mastering regex power
Basta mastering regex powerBasta mastering regex power
Basta mastering regex power
 
Formal methods 3 - languages and machines
Formal methods   3 - languages and machinesFormal methods   3 - languages and machines
Formal methods 3 - languages and machines
 
Perl Intro 7 Subroutines
Perl Intro 7 SubroutinesPerl Intro 7 Subroutines
Perl Intro 7 Subroutines
 
Grep - A powerful search utility
Grep - A powerful search utilityGrep - A powerful search utility
Grep - A powerful search utility
 
Perl Intro 2 First Program
Perl Intro 2 First ProgramPerl Intro 2 First Program
Perl Intro 2 First Program
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regex
 

Perl Intro 5 Regex Matches And Substitutions

  • 1. Perl Brown Bag Regular Expressions Matching Substitution Shaun Griffith April 10, 2006 05/13/12 ATI Confidential 1
  • 3. Regular Expressions Computer Science gives us the term Regular Expression, the topic concerned with deciding if a given string matches a pattern. “Regular” refers to a type of formal language. It turns out that Perl’s regular expressions aren’t regular. What does that mean? Perl is more powerful than regular expressions. (But we still call them regexes.) Regular expressions are used in compilers, parsers, password checkers, web form checkers, and of course datalog yield reporters. 05/13/12 3
  • 4. Meta Characters Metacharacters are special characters 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 05/13/12 4
  • 5. Quantifiers Quantifiers: how many? * Match 0 or more times + Match 1 or more times ? Match 1 or 0 times {n} Match exactly n times {n,} Match at least n times {n,m} Match between n and m times ( n,m <=32766) 05/13/12 5
  • 6. Special Characters Stuff That’s Hard to Type t tab n newline r return (carriage return) f form feed e escape 033 octal char x1B hex char c[ control char 05/13/12 6
  • 7. Character Classes Regex Pattern Shortcuts w Match a "word" character (alphanumeric plus "_") W Match a non-"word" character s Match a whitespace character S Match a non-whitespace character d Match a digit character D Match a non-digit character 05/13/12 7
  • 8. Character Classes… More on Character Classes: ^ As the first char, negates the class ( [^A] matches anything except “A” ) Otherwise literal caret. - between 2 chars is a range ( [A-G] will match A,B,C,D,E,F, or G ) otherwise literal dash b backspace (easiest way to get one) ] as first char, literal close bracket wWsSdD can all be used in a character class (but not in a range) 05/13/12 8
  • 9. Zero Width Characters These don’t take up any space: b Match a word boundary B Match a non-(word boundary) A Match only at beginning of string Z Match only at end of string, or before newline at the end z Match only at end of string 05/13/12 9
  • 10. Regex Engine Modifiers Change how the pattern works: /i ignore case /g return all matches /s period (.) now matches newline (n) /m “multi-line”: ^ and $ match near any n, not just start/end of string /x “expanded” regex: space and newlines are ignored, and # comments allowed: Example: /w+ # first name s+ # space d+ # age /x 05/13/12 10
  • 11. Matching and Capturing Basic Matching: $var =~ m/something here/; Capturing: $var =~ m/(pattern)/; $1 contains “pattern” $catch = $1; or ($catch) = $var =~ m/(pattern)/; ($x,$y) = $var =~ m/(first) (second)/; Backreferences: refer to previous match $var = “The red red book.”; $var =~ m/(red) 1/; 05/13/12 11
  • 12. Substitutions Basic Substitutions: $var =~ s/pattern/replacement/; “this” is any m/pattern/ “that” is a double-quoted string For example $var =~ s/(first) (last)/$2 $1/; Backreferences belong in pattern only! Inline captures don’t work with s///: ($x) = $var =~ s/(this)/that/; # wrong Use $1 instead: $x = $1; 05/13/12 12
  • 13. Putting It All Together Wacky Test Names while (<>) { # FREQ BYP4 TRY if ( m/mpll_(d+)_(byp|by4)_(w)/i ) { $data{$die}{FREQ} = $1; $data{$die}{BYP4} = $2; $data{$die}{TRY} = $3; next; } } 05/13/12 13
  • 14. Next Time? FTP? •Using Net::FTP •Navigating Remotely •Moving Files Subroutines? •Passing parameters •Catching parameters •Returning data Filehandles? •Open •Close •EOF 05/13/12 14