Modules

  • ABCDE
  • FGHIL
  • MNOPS
  • TUX

Tools

perlreref

Perl 5 version 8.8 documentation
Recently read

perlreref

NAME

perlreref - Perl Regular Expressions Reference

DESCRIPTION

This is a quick reference to Perl's regular expressions. For full information see perlre and perlop, as well as the SEE ALSO section in this document.

OPERATORS

  1. =~ determines to which variable the regex is applied.
  2. In its absence, $_ is used.
  3. $var =~ /foo/;
  4. !~ determines to which variable the regex is applied,
  5. and negates the result of the match; it returns
  6. false if the match succeeds, and true if it fails.
  7. $var !~ /foo/;
  8. m/pattern/igmsoxc searches a string for a pattern match,
  9. applying the given options.
  10. i case-Insensitive
  11. g Global - all occurrences
  12. m Multiline mode - ^ and $ match internal lines
  13. s match as a Single line - . matches \n
  14. o compile pattern Once
  15. x eXtended legibility - free whitespace and comments
  16. c don't reset pos on failed matches when using /g
  17. If 'pattern' is an empty string, the last I<successfully> matched
  18. regex is used. Delimiters other than '/' may be used for both this
  19. operator and the following ones.
  20. qr/pattern/imsox lets you store a regex in a variable,
  21. or pass one around. Modifiers as for m// and are stored
  22. within the regex.
  23. s/pattern/replacement/igmsoxe substitutes matches of
  24. 'pattern' with 'replacement'. Modifiers as for m//
  25. with one addition:
  26. e Evaluate replacement as an expression
  27. 'e' may be specified multiple times. 'replacement' is interpreted
  28. as a double quoted string unless a single-quote (') is the delimiter.
  29. ?pattern? is like m/pattern/ but matches only once. No alternate
  30. delimiters can be used. Must be reset with L<reset|perlfunc/reset>.

SYNTAX

  1. \ Escapes the character immediately following it
  2. . Matches any single character except a newline (unless /s is used)
  3. ^ Matches at the beginning of the string (or line, if /m is used)
  4. $ Matches at the end of the string (or line, if /m is used)
  5. * Matches the preceding element 0 or more times
  6. + Matches the preceding element 1 or more times
  7. ? Matches the preceding element 0 or 1 times
  8. {...} Specifies a range of occurrences for the element preceding it
  9. [...] Matches any one of the characters contained within the brackets
  10. (...) Groups subexpressions for capturing to $1, $2...
  11. (?:...) Groups subexpressions without capturing (cluster)
  12. | Matches either the subexpression preceding or following it
  13. \1, \2 ... The text from the Nth group

ESCAPE SEQUENCES

These work as in normal strings.

  1. \a Alarm (beep)
  2. \e Escape
  3. \f Formfeed
  4. \n Newline
  5. \r Carriage return
  6. \t Tab
  7. \037 Any octal ASCII value
  8. \x7f Any hexadecimal ASCII value
  9. \x{263a} A wide hexadecimal value
  10. \cx Control-x
  11. \N{name} A named character
  12. \l Lowercase next character
  13. \u Titlecase next character
  14. \L Lowercase until \E
  15. \U Uppercase until \E
  16. \Q Disable pattern metacharacters until \E
  17. \E End case modification

For Titlecase, see Titlecase.

This one works differently from normal strings:

  1. \b An assertion, not backspace, except in a character class

CHARACTER CLASSES

  1. [amy] Match 'a', 'm' or 'y'
  2. [f-j] Dash specifies "range"
  3. [f-j-] Dash escaped or at start or end means 'dash'
  4. [^f-j] Caret indicates "match any character _except_ these"

The following sequences work within or without a character class. The first six are locale aware, all are Unicode aware. The default character class equivalent are given. See perllocale and perlunicode for details.

  1. \d A digit [0-9]
  2. \D A nondigit [^0-9]
  3. \w A word character [a-zA-Z0-9_]
  4. \W A non-word character [^a-zA-Z0-9_]
  5. \s A whitespace character [ \t\n\r\f]
  6. \S A non-whitespace character [^ \t\n\r\f]
  7. \C Match a byte (with Unicode, '.' matches a character)
  8. \pP Match P-named (Unicode) property
  9. \p{...} Match Unicode property with long name
  10. \PP Match non-P
  11. \P{...} Match lack of Unicode property with long name
  12. \X Match extended unicode sequence

POSIX character classes and their Unicode and Perl equivalents:

  1. alnum IsAlnum Alphanumeric
  2. alpha IsAlpha Alphabetic
  3. ascii IsASCII Any ASCII char
  4. blank IsSpace [ \t] Horizontal whitespace (GNU extension)
  5. cntrl IsCntrl Control characters
  6. digit IsDigit \d Digits
  7. graph IsGraph Alphanumeric and punctuation
  8. lower IsLower Lowercase chars (locale and Unicode aware)
  9. print IsPrint Alphanumeric, punct, and space
  10. punct IsPunct Punctuation
  11. space IsSpace [\s\ck] Whitespace
  12. IsSpacePerl \s Perl's whitespace definition
  13. upper IsUpper Uppercase chars (locale and Unicode aware)
  14. word IsWord \w Alphanumeric plus _ (Perl extension)
  15. xdigit IsXDigit [0-9A-Fa-f] Hexadecimal digit

Within a character class:

  1. POSIX traditional Unicode
  2. [:digit:] \d \p{IsDigit}
  3. [:^digit:] \D \P{IsDigit}

ANCHORS

All are zero-width assertions.

  1. ^ Match string start (or line, if /m is used)
  2. $ Match string end (or line, if /m is used) or before newline
  3. \b Match word boundary (between \w and \W)
  4. \B Match except at word boundary (between \w and \w or \W and \W)
  5. \A Match string start (regardless of /m)
  6. \Z Match string end (before optional newline)
  7. \z Match absolute string end
  8. \G Match where previous m//g left off

QUANTIFIERS

Quantifiers are greedy by default -- match the longest leftmost.

  1. Maximal Minimal Allowed range
  2. ------- ------- -------------
  3. {n,m} {n,m}? Must occur at least n times but no more than m times
  4. {n,} {n,}? Must occur at least n times
  5. {n} {n}? Must occur exactly n times
  6. * *? 0 or more times (same as {0,})
  7. + +? 1 or more times (same as {1,})
  8. ? ?? 0 or 1 time (same as {0,1})

There is no quantifier {,n} -- that gets understood as a literal string.

EXTENDED CONSTRUCTS

  1. (?#text) A comment
  2. (?imxs-imsx:...) Enable/disable option (as per m// modifiers)
  3. (?=...) Zero-width positive lookahead assertion
  4. (?!...) Zero-width negative lookahead assertion
  5. (?<=...) Zero-width positive lookbehind assertion
  6. (?<!...) Zero-width negative lookbehind assertion
  7. (?>...) Grab what we can, prohibit backtracking
  8. (?{ code }) Embedded code, return value becomes $^R
  9. (??{ code }) Dynamic regex, return value used as regex
  10. (?(cond)yes|no) cond being integer corresponding to capturing parens
  11. (?(cond)yes) or a lookaround/eval zero-width assertion

VARIABLES

  1. $_ Default variable for operators to use
  2. $* Enable multiline matching (deprecated; not in 5.9.0 or later)
  3. $& Entire matched string
  4. $` Everything prior to matched string
  5. $' Everything after to matched string

The use of those last three will slow down all regex use within your program. Consult perlvar for @LAST_MATCH_START to see equivalent expressions that won't cause slow down. See also Devel::SawAmpersand.

  1. $1, $2 ... hold the Xth captured expr
  2. $+ Last parenthesized pattern match
  3. $^N Holds the most recently closed capture
  4. $^R Holds the result of the last (?{...}) expr
  5. @- Offsets of starts of groups. $-[0] holds start of whole match
  6. @+ Offsets of ends of groups. $+[0] holds end of whole match

Captured groups are numbered according to their opening paren.

FUNCTIONS

  1. lc Lowercase a string
  2. lcfirst Lowercase first char of a string
  3. uc Uppercase a string
  4. ucfirst Titlecase first char of a string
  5. pos Return or set current match position
  6. quotemeta Quote metacharacters
  7. reset Reset ?pattern? status
  8. study Analyze string for optimizing matching
  9. split Use regex to split a string into parts

The first four of these are like the escape sequences \L , \l , \U , and \u . For Titlecase, see Titlecase.

TERMINOLOGY

Titlecase

Unicode concept which most often is equal to uppercase, but for certain characters like the German "sharp s" there is a difference.

AUTHOR

Iain Truskett.

This document may be distributed under the same terms as Perl itself.

SEE ALSO

THANKS

David P.C. Wollmann, Richard Soderberg, Sean M. Burke, Tom Christiansen, Jim Cromie, and Jeffrey Goff for useful advice.