B. Summary of Regular Expressions
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.
/abc/
Matches abc anywhere within the string
/^abc/
Matches abc at the beginning of the string
/abc$/
Matches abc at the end of the string
/a|b/
Matches either a or b Can also be used with words (i.e., /perl|tcl/)
/ab{2,4}c/
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.
/ab*c/
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/.
/ab+c/
Matches an a followed by one or more b's followed by c. Same as /ab{1,}c/.
/ab?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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access