Download presentation
Presentation is loading. Please wait.
Published byGarrison Waye Modified over 9 years ago
1
Regular Expressions in Perl By Josue Vazquez
2
What are Regular Expressions? A template that either matches or doesn’t match a given string. Often called a pattern in Perl.
3
Using Simple Patterns To match a pattern against the contents of a string, put a pair of forward slashes (/). Almost always found in the conditional expression of if or while. All of the usual backslash escapes are available in patterns
4
Metacharacters Special characters that have a special meaning in regular expressions. Example the dot (.) is a wildcard character, it matches any single character except a newline.
5
Metacharacters (Con’t) CharacterMeaning ^Beginning of string $End of string.Any character except newline \Quote or special
6
Simple Quantifiers Three main quantifiers: Star (*) Plus (+) Question mark (?)
7
Star (*) Quantifier 1. Matches the preceding item zero or more times. Example: /fred\t*barney/.* will match any character, any number of times. Example: /fred.*barney/
8
Plus (+) Quantifier Matches the proceeding item one or more times. Example: /fred +barney/
9
Question Mark (?) Quantifier Makes the preceding item optional Example: /bam-?bam/
10
Grouping in Patterns Parentheses may be used for grouping.
11
Back reference Refers to text that we matched in the parentheses Denote a back reference with a backslash followed by a number. Back reference does not have to be right next to the parentheses group.
12
Alternatives Described by the vertical bar (|) means that either the left side may match or the right side. Can also make patterns.
13
Character Classes A list of possible characters inside square brackets. ([]) Matches any single character from within the class. Matches just one character but can be any one listed in the class.
14
Character Classes (Con’t) May use hyphen to specify a range of characters. A caret(^) at the start of the character class negates it.
15
Character Class Shortcuts Some character classes appear frequently so they have shortcuts SymbolMeaningAs Bytes \dDigit[0-9] \sWhitespace[ \t\n\r\f] \wWord character[a-zA-Z0-9_]
16
Negating the Shortcuts To negate the shortcuts just use the uppercase counter parts. SymbolMeaningAs Bytes \DNon-digit[^0-9] \SNon-whitespace[ ^ \t\n\r\f] \WNon-word character[^a-zA-Z0-9_]
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.