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

Regex Cheat Sheet in Perl

Here's a concise regex cheat sheet tailored for Perl:

Basic Symbols:

  • .: Matches any character except a newline.
  • ^: Matches the start of a string.
  • $: Matches the end of a string.

Quantifiers:

  • *: 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.

Character Classes:

  • [...]: 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.

Special Characters:

To match these characters literally, escape them with a backslash (\).

  • . ^ $ * + ? { } [ ] \ | ( )

Grouping and Capturing:

  • (...): Groups several characters together and captures the matched text.
  • (?:...): Groups characters without capturing.

Lookaround Assertions:

  • (?=...): Positive lookahead.
  • (?!...): Negative lookahead.
  • (?<=...): Positive lookbehind.
  • (?<!...): Negative lookbehind.

Modifiers:

  • 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).

Perl-Specific Regex Operations:

  • =~: Binding operator to apply regex.
  • !~: Binding operator for non-matching.
  • //: Regex delimiter.
  • $1, $2, ...: Captured groups.
  • $&: Matched string.
  • `$`` (back-tick): Pre-match string.
  • $' (single-quote): Post-match string.

Examples:

  • Case-insensitive search:
if ($str =~ /hello/i) {
    print "Found 'hello'\n";
}
  • Capture text:
if ($str =~ /(\d+)/) {
    print "Found number: $1\n";
}
  • Negative lookahead:
if ($str =~ /foo(?!bar)/) {
    print "Found 'foo' not followed by 'bar'\n";
}

Summary:

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.

  1. Perl regex pattern examples cheat sheet:

    • Description: Examples of common regex patterns for various scenarios.
    • 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
      
  2. Regex metacharacters and escapes in Perl:

    • Description: Metacharacters have special meanings in regex. Use \ to escape them if you want to match them literally.
    • Cheat Sheet:
      .  ^  $  *  +  ?  {  }  [  ]  (  )  \  |
      
  3. Character classes cheat sheet for Perl:

    • Description: Character classes allow you to match a set of characters.
    • Cheat Sheet:
      \d      # Digit (0-9)
      \D      # Non-digit
      \w      # Word character (alphanumeric + underscore)
      \W      # Non-word character
      \s      # Whitespace character
      \S      # Non-whitespace character
      
  4. Quantifiers and modifiers in Perl regex cheat sheet:

    • Description: Quantifiers specify how many times a character or group should be repeated.
    • 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
      
  5. Lookahead and lookbehind examples in Perl regex:

    • Description: Lookahead and lookbehind are zero-width assertions that check for patterns without consuming characters.
    • Examples:
      /foo(?=bar)/         # Matches "foo" only if followed by "bar"
      /(?<=foo)bar/        # Matches "bar" only if preceded by "foo"
      
  6. Common regex tasks and shortcuts in Perl:

    • Description: Shortcuts and tips for common regex tasks.
    • Cheat Sheet:
      \b     # Word boundary
      \A     # Start of the string
      \Z     # End of the string
      .+?    # Non-greedy match
      
  7. Perl regex capture groups cheat sheet:

    • Description: Capture groups allow you to extract portions of the matched text.
    • Cheat Sheet:
      /(\d{2})-(\d{2})-(\d{4})/   # Capturing date components