SlideServe
Menu
Préférences
© SlideServe · v2026

Regular Expression: Pattern Matching

thora avatar
7 diapositives · 267 Views · Sep 03, 2014

Regular Expression: Pattern Matching. Introduction. What is Regular Expression? Grammar /rule for matching pattern in strings pattern = a sequence of characters Pattern matching Syntax $target =~ / $pattern / ; searches $target for $pattern true if found, false otherwise

Regular Expression: Pattern Matching - slide 1
01 / 7Regular Expression: Pattern Matching
1 / 17

Télécharger la présentation

Regular Expression: Pattern Matching
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Regular Expression:Pattern Matching Web Programming

  2. Introduction • What is Regular Expression? • Grammar /rule for matching pattern in strings • pattern = a sequence of characters • Pattern matching • Syntax • $target=~ /$pattern/; • searches $target for $pattern • true if found, false otherwise • ( “prog1.pl” =~ /pl/) : true • ( “prog1.pl” =~ /PL/) : false • can use any pattern delimiter character with m • e.g. $target =~ m!$pattern! (or m|$pattern|) • $target!~ /$pattern/; • true if NOT found, false otherwise Example script Web Programming

  3. Matching Repeats • + Match one or more preceding characters • e.g. /ab+c/ matches ‘abc’, ‘abbc’, ‘abbbc’, etc. • *  Match zero or more preceding characters • e.g. /ab*c/ matches ‘ac’, ‘abc’, ‘abbc’, ‘abbbc’, etc. • ?  Match zero or one preceding character • e.g. /ab?c/ matches ‘ac’, or ‘abc’. • . Match any character • e.g. /b.a/ matches ‘abba’, ‘b1a’, etc. /b.*a/ matches ‘aba’, ‘abba’, ‘banana’, etc. • ‘.’ does not match newline (i.e. \n) • /b.*a/ does not match ‘ab\na’, ‘abb\na’, etc.  Regular Expression Checker: Perl script, HTML Form, CGI Web Programming

  4. Special Characters • [$pattern]  Match any character in $pattern • e.g. /[Rr]ed/ matches ‘bredd’, ‘bRedd’, ‘red’, ‘Red’, etc. /[0-9]/ matches any number /[a-zA-Z]/ matches any alphabet /[0-9a-zA-z]/ matches any alphanumeric • [^$pattern]  Match any character except those in $pattern • e.g. /[^0-9]/ matches any non-numeric characters. • \b  Match at word boundary • Any character other than alphanumeric and underscore (i.e., [0-9a-zA-Z_]) • e.g. /\bred\b/ matches ‘is red’, ‘red rose’, ‘$red’  Example: HTML Form, CGI Web Programming

  5. Escape Sequences • \d • any digit (i.e. [0-9] ) • \D • any non-digit (i.e. [^0-9]) • \w • any word character (i.e. [_0-9a-zA-Z]) • \W • any non-word character (i.e. [^_0-9a-zA-Z]) • \s • any white space (i.e. [ \r\t\n\f]) • \S • any non-white space (i.e. [^ \r\t\n\f]) Web Programming

  6. Pattern Matching Options • /$pattern/i • Ignore case • e.g. /ab/i matches ‘ab’, ‘AB’, ‘Ab’, ‘aB’ • /$pattern/g • Matches all possible patterns • Returns a list of matches • e.g. @matches = ‘abcdcb’=~/.b/g; @matches will be (‘ab’,’cb’)  Example: HTML Form, CGI Web Programming

  7. Substitution • $string =~ s/$pattern/$replacement/; • Replace $pattern with $replacement in $string$string = “Before substitution”;$string =~ s/Before/After/; “After substitution” • Substitution Options • $string =~ s/$pattern/$replacement/i; • Ignore case of $pattern $string = “One plus one is done.”;$string =~ s/one/ONE/i;  “ONE plus one is done.” • $string =~ s/$pattern/$replacement/g; • Change all occurrence of $pattern in $string $string = “ this is line 1. ”;$string =~ s/ +/ /g; “ this is a line. ” • $string =~ s/$pattern/$replacement/s; • Treat $string as a single line (i.e., . will match \n) $string = “this <img src=\n img.gif>image”;$string =~ s/<.+>//s;  “this image” Example Web Programming