One of the most powerful features of Perl is its regular expression handling. Regular expressions are especially useful for CGI programming, as text manipulation is central to so many CGI applications. In this appendix, we include a quick reference to regular expressions in Perl. For more information on Perl, see the Nutshell Handbooks Learning Perl by Randal L. Schwartz, Programming Perl by Larry Wall and Randal L. Schwartz, and Perl 5 Desktop Reference by Johan Vromans, all published by O'Reilly & Associates, Inc.
Matches abc anywhere within the string
Matches abc at the beginning of the string
Matches abc at the end of the string
Matches either a or b Can also be used with words (i.e., /perl|tcl/)
Matches an a followed by 2-4 b's, followed by c. If the second number is omitted, such as /ab {2,}c/, the expression will match two or more b's.
Matches an a followed by zero or more b's, followed by c. Expressions are greedy--it will match as many as possible. Same as /ab{0,}c/.
Matches an a followed by one or more b's followed by c. Same as /ab{1,}c/.
Matches an a followed by an optional b followed by c Same as /ab{0,1}c/. This has a different meaning in Perl 5. In Perl 5, the expression: /ab*?c/matches an a followed by as few b's as possible (non-greedy).
Matches any single character except a newline (\n) /p..l / matches a p followed by any two characters, followed by l, so it will match such strings as perl, pall, pdgl, p3gl, etc.
A character class--matches any one of the three characters listed. A pattern of /[abc]+/ matches strings such as abcab, acbc, abbac, aaa, abcacbac, ccc, etc.
Matches a digit. Same as /[0-9]/Multipliers can be used (/\d+/ matches one or more digits)
Matches a character classified as a word. Same as /[a-zA-Z0-9_]/
Matches a character classified as whitespace. Same as /[ \r\t\n\f]/
Matches a word boundary or a backspace/test\b/ matches test, but not testing. However, \b matches a backspace character inside a class (i.e., [\b])
Matches a character that is not in the class/[^abc ]+/ will match such strings as hello, test, perl, etc.
Matches a character that is not a digit. Same as /[^0-9]/
Matches a character that is not a word. Same as /[^a-zA-Z0-9_]/
Matches a character that is not whitespace. Same as /[^ \r\t\n\f]/
Requires that there is no word boundary/hello\B/ matches hello, but not hello there
Matches the * character. Use the \ character to escape characters that have significance in a regular expression.
Matches abc anywhere within the string, but the parentheses act as memory, storing abc in the variable $1.
/name=(.*)/ will store zero or more characters after name= in variable $1.
/name=(.*)&user=\1/ will store zero or more characters after name= in $1. Then, Perl will replace \1 with the value in $1, and check to see if the pattern matches.
/name=([^&]*)/ will store zero or more characters after name= but before the & character in variable $1.
/name=([^&]+)&age=(.*)$/ will store one or more characters after name= but before & in $1. It then matches the & character. All characters after age= but before the end of the line are stored in $2.
Ignores case. Matches either abc, Abc, ABC, aBc, aBC, etc.
This HTML Help has been published using the chm2web software. |