Perl Tutorial
Fundamentals
Input and Output
Control Flow
Arrays and Lists
Hash
Scalars
Strings
Object Oriented Programming in Perl
Subroutines
Regular Expressions
File Handling
Context Sensitivity
CGI Programming
Misc
Here's a concise regex cheat sheet tailored for Perl:
.
: Matches any character except a newline.^
: Matches the start of a string.$
: Matches the end of a string.*
: Matches the preceding item 0 or more times.+
: Matches the preceding item 1 or more times.?
: Matches the preceding item 0 or 1 times.{n}
: Matches exactly n occurrences of the preceding item.{n,}
: Matches n or more occurrences of the preceding item.{n,m}
: Matches between n and m occurrences of the preceding item.[...]
: Matches any one of the characters inside the brackets.[^...]
: Matches any character not inside the brackets.\d
: Matches any digit. Equivalent to [0-9]
.\D
: Matches any non-digit.\w
: Matches any word character (alphanumeric plus underscore). Equivalent to [a-zA-Z0-9_]
.\W
: Matches any non-word character.\s
: Matches any whitespace character (spaces, tabs, line breaks).\S
: Matches any non-whitespace character.To match these characters literally, escape them with a backslash (\
).
.
^
$
*
+
?
{
}
[
]
\
|
(
)
(...)
: Groups several characters together and captures the matched text.(?:...)
: Groups characters without capturing.(?=...)
: Positive lookahead.(?!...)
: Negative lookahead.(?<=...)
: Positive lookbehind.(?<!...)
: Negative lookbehind.i
: Case-insensitive search.m
: Treats the string as multiple lines (^
and $
match the start/end of lines).s
: Treats the string as a single line (.
matches even newline \n
).x
: Extended mode (allows spaces and comments in the pattern).=~
: Binding operator to apply regex.!~
: Binding operator for non-matching.//
: Regex delimiter.$1, $2, ...
: Captured groups.$&
: Matched string.$'
(single-quote): Post-match string.if ($str =~ /hello/i) { print "Found 'hello'\n"; }
if ($str =~ /(\d+)/) { print "Found number: $1\n"; }
if ($str =~ /foo(?!bar)/) { print "Found 'foo' not followed by 'bar'\n"; }
This cheat sheet provides a quick overview of regex syntax in Perl. Regular expressions are vast and powerful, so this guide is by no means exhaustive, but it should serve as a starting point or a quick reference for Perl developers.
Perl regex pattern examples cheat sheet:
/^\d{3}-\d{2}-\d{4}$/ # Social Security Number /\b\d{4}-\d{2}-\d{2}\b/ # Date (YYYY-MM-DD) /[A-Za-z]+/ # Alphabetic word /\b\d+\b/ # Any number
Regex metacharacters and escapes in Perl:
\
to escape them if you want to match them literally.. ^ $ * + ? { } [ ] ( ) \ |
Character classes cheat sheet for Perl:
\d # Digit (0-9) \D # Non-digit \w # Word character (alphanumeric + underscore) \W # Non-word character \s # Whitespace character \S # Non-whitespace character
Quantifiers and modifiers in Perl regex cheat sheet:
* # 0 or more + # 1 or more ? # 0 or 1 {n} # Exactly n times {n,} # n or more times {n,m} # Between n and m times
Lookahead and lookbehind examples in Perl regex:
/foo(?=bar)/ # Matches "foo" only if followed by "bar" /(?<=foo)bar/ # Matches "bar" only if preceded by "foo"
Common regex tasks and shortcuts in Perl:
\b # Word boundary \A # Start of the string \Z # End of the string .+? # Non-greedy match
Perl regex capture groups cheat sheet:
/(\d{2})-(\d{2})-(\d{4})/ # Capturing date components