Updated: 2016-02-05-Fri 04:13:21 UTC

Perl Regex Examples:


back to Perl REGEX reference overview page
The Digits class (\d):
Note: The digits class includes 0-9; however, it also
includes the digits of other numeral systems for
Unicode-configured sysetms
(e.g., the chinese numerals: 一,二,三, etc.).
The non-digits class is the compliment to the digits class
(everything in the configured character set except digits).
# REGEX Pattern:
    Bound String that is tested...
  • $`=""
  • $&=""
  • &'=""
01. /\d/
    "3"
  • $`=""
  • $&="3"
  • &'=""
02. /\d/
    "34"
  • $`=""
  • $&="3"
  • &'="4"
03. /\d/
    "-42"
  • $`="-"
  • $&="4"
  • &'="2"
04. /\d/
    "-.14"
  • $`="-."
  • $&="1"
  • &'="4"
05. /\d./
    "-3.14"
  • $`="-"
  • $&="3."
  • &'="14"
06. /^\d/
    "-3.14"
  • $`=undef
  • $&=undef
  • &'=undef
Note: Match FAILED (match variables are undefined).
07. /\d\d/
    "-3.14"
  • $`="-3."
  • $&="14"
  • &'=""
08. /.\d/
    "-3.14"
  • $`=""
  • $&="-3"
  • &'=".14"
09. /\d\d./
    "-3.14"
  • $`=undef
  • $&=undef
  • &'=undef
Note: Match FAILED (match variables are undefined).
10. /\d\d.?/
    "-3.14"
  • $`="-3."
  • $&="14"
  • &'=""
11. /\d*/
    "-3.14"
  • $`=""
  • $&=""
  • &'="-3.14"
Note: evaluation engine does not find a match at the first character '-',
so it simply accepts a match of 0 digits. This might seem contrary to
to the notion of greediness (i.e., you'd assume it would match at least
one digit further down the string, but it likely doesn't because this
would just be more runtime (and why waste time when it can match 0).
12. /\d\d*/
    "-3.14"
  • $`="-"
  • $&="3"
  • &'=".14"
Note: evaluation engine does not find a match at the first character '-',
so it moves on and matches the first digit on the '3', then for the
second digit, it simply accepts a match of 0 digits. This might seem
contrary to the notion of greediness (i.e., you might wonder if it would
try for a 'better' match at '14', but it likely doesn't because this
would just be more runtime (and why waste time when it can match 0
digits for the second digit (\d*).
13. /\d\d?/
    "-3.14"
  • $`="-"
  • $&="3"
  • &'=".14"
Note: See previous example for similar grounds as to why it might
not match where you might expect it to later in the string.
14. /\d+/
    "-3.14"
  • $`="-"
  • $&="3"
  • &'=".14"
15. /\d\d|\d/
    "-3.14"
  • $`="-"
  • $&="3"
  • &'=".14"
16. /\d|\d\d/
    "-3.14"
  • $`="-"
  • $&="3"
  • &'=".14"
17. /\D/
    "-3.14"
  • $`=""
  • $&="-"
  • &'="3.14"
18. /\D\D/
    "-3.14"
  • $`=undef
  • $&=undef
  • &'=undef
Note: Match FAILED (match variables are undefined).
19. /\D\D?/
    "-3.14"
  • $`=""
  • $&="-"
  • &'="3.14"
20. /\D\d/
    "-3.14"
  • $`=""
  • $&="-3"
  • &'=".14"
21. /\d\D/
    "-3.14"
  • $`="-"
  • $&="3."
  • &'="14"
22. /\d\d\D/
    "-3.14"
  • $`=undef
  • $&=undef
  • &'=undef
Note: Match FAILED (match variables are undefined).
23. /(\d\d\D)?/
    "-3.14"
  • $`=""
  • $&=""
  • &'="-3.14"
24. /\d\d?\D/
    "-3.14"
  • $`="-"
  • $&="3."
  • &'="14"
25. /(\d\d)?\D/
    "-3.14"
  • $`=""
  • $&="-"
  • &'="3.14"
26. /^\d\D/
    "-3.14"
  • $`=undef
  • $&=undef
  • &'=undef
Note: Match FAILED (match variables are undefined).
27. /\D$/
    "-3.14"
  • $`=undef
  • $&=undef
  • &'=undef
Note: Match FAILED (match variables are undefined).
Back to menu (top)
The Word class (\w):
Note: TODO
# REGEX Pattern:
    Bound String that is tested...
  • $`=""
  • $&=""
  • &'=""
01. /\w/
    "-3.14"
  • $`="-"
  • $&="3"
  • &'=".14"
02. /\w+/
    "-3.14"
  • $`="-"
  • $&="3"
  • &'=".14"
03. /\w*/
    "-3.14"
  • $`=""
  • $&=""
  • &'="-3.14"
04. /\w\w*/
    "-3.14"
  • $`="-"
  • $&="3"
  • &'=".14"
05. /\w*\w/
    "-3.14"
  • $`="-"
  • $&="3"
  • &'=".14"
06. /\w+\w/
    "-3.14"
  • $`="-3."
  • $&="14"
  • &'=""
07. /\w+\d/
    "-3.14"
  • $`="-3."
  • $&="14"
  • &'=""
08. /\d\w/
    "-3.14"
  • $`="-3."
  • $&="14"
  • &'=""
09. /\w\D/
    "-3.14"
  • $`="-"
  • $&="3."
  • &'="14"
Back to menu (top)
The word bounday and non-word boundary classes (\b & \B):
Note:
# REGEX Pattern:
    Bound String that is tested...
  • $`=""
  • $&=""
  • &'=""
01. /show\b/
    "It is full of Python heritage that shall never wither in time; therefore, 'At Last the 1948 Show' is one of my favorite shows."
  • $`=undef
  • $&=undef
  • &'=undef
Note: Match FAILED (match variables are undefined).
02. /\sshow/
    "It is full of Python heritage that shall never wither in time; therefore, 'At Last the 1948 Show' is one of my favorite shows."
  • $`="It is full of Python heritage that shall never wither in time; therefore, 'At Last the 1948 Show' is one of my favorite"
  • $&=" show"
  • &'="s."
03. /\b\d{4}/
    "It is full of Python heritage that shall never wither in time; therefore, 'At Last the 1948 Show' is one of my favorite shows."
  • $`="It is full of Python heritage that shall never wither in time; therefore, 'At Last the "
  • $&="1948"
  • &'=" Show' is one of my favorite shows."
04. /\b\d{4}\b/
    "It is full of Python heritage that shall never wither in time; therefore, 'At Last the 1948 Show' is one of my favorite shows."
  • $`="It is full of Python heritage that shall never wither in time; therefore, 'At Last the "
  • $&="1948"
  • &'=" Show' is one of my favorite shows."
05. /\b\d.*/
    "It is full of Python heritage that shall never wither in time; therefore, 'At Last the 1948 Show' is one of my favorite shows."
  • $`="It is full of Python heritage that shall never wither in time; therefore, 'At Last the "
  • $&="1948 Show' is one of my favorite shows."
  • &'=""
06. /\B\d.*/
    "It is full of Python heritage that shall never wither in time; therefore, 'At Last the 1948 Show' is one of my favorite shows."
  • $`="It is full of Python heritage that shall never wither in time; therefore, 'At Last the 1"
  • $&="948 Show' is one of my favorite shows."
  • &'=""
07. /\b\d.*\b/
    "It is full of Python heritage that shall never wither in time; therefore, 'At Last the 1948 Show' is one of my favorite shows."
  • $`="It is full of Python heritage that shall never wither in time; therefore, 'At Last the "
  • $&="1948 Show' is one of my favorite shows"
  • &'="."
08. /\s\d\b/
    "It is full of Python heritage that shall never wither in time; therefore, 'At Last the 1948 Show' is one of my favorite shows."
  • $`=undef
  • $&=undef
  • &'=undef
Note: Match FAILED (match variables are undefined).
09. /\s\w+\b/
    "It is full of Python heritage that shall never wither in time; therefore, 'At Last the 1948 Show' is one of my favorite shows."
  • $`="It"
  • $&=" is"
  • &'=" full of Python heritage that shall never wither in time; therefore, 'At Last the 1948 Show' is one of my favorite shows."
10. /\b\d*\b/
    "It is full of Python heritage that shall never wither in time; therefore, 'At Last the 1948 Show' is one of my favorite shows."
  • $`=""
  • $&=""
  • &'="It is full of Python heritage that shall never wither in time; therefore, 'At Last the 1948 Show' is one of my favorite shows."
11. /\b\d+\b/
    "It is full of Python heritage that shall never wither in time; therefore, 'At Last the 1948 Show' is one of my favorite shows."
  • $`="It is full of Python heritage that shall never wither in time; therefore, 'At Last the "
  • $&="1948"
  • &'=" Show' is one of my favorite shows."
12. /\b\w*\b/
    "It is full of Python heritage that shall never wither in time; therefore, 'At Last the 1948 Show' is one of my favorite shows."
  • $`=""
  • $&="It"
  • &'=" is full of Python heritage that shall never wither in time; therefore, 'At Last the 1948 Show' is one of my favorite shows."
13. /\b\w+\d+\b/
    "It is full of Python heritage that shall never wither in time; therefore, 'At Last the 1948 Show' is one of my favorite shows."
  • $`="It is full of Python heritage that shall never wither in time; therefore, 'At Last the "
  • $&="1948"
  • &'=" Show' is one of my favorite shows."
14. /\b\w\d\d\w*\b/
    "It is full of Python heritage that shall never wither in time; therefore, 'At Last the 1948 Show' is one of my favorite shows."
  • $`="It is full of Python heritage that shall never wither in time; therefore, 'At Last the "
  • $&="1948"
  • &'=" Show' is one of my favorite shows."
15. /\bhow\b/
    "It is full of Python heritage that shall never wither in time; therefore, 'At Last the 1948 Show' is one of my favorite shows."
  • $`=undef
  • $&=undef
  • &'=undef
Note: Match FAILED (match variables are undefined).
16. /\bshow\b/
    "It is full of Python heritage that shall never wither in time; therefore, 'At Last the 1948 Show' is one of my favorite shows."
  • $`=""
  • $&=""
  • &'=""
Note: Match FAILED (match variables are undefined).
17. /\bshow/
    "It is full of Python heritage that shall never wither in time; therefore, 'At Last the 1948 Show' is one of my favorite shows."
  • $`="It is full of Python heritage that shall never wither in time; therefore, 'At Last the 1948 Show' is one of my favorite "
  • $&="show"
  • &'="s."
18. /show\b/
    "It is full of Python heritage that shall never wither in time; therefore, 'At Last the 1948 Show' is one of my favorite shows."
  • $`=""
  • $&=""
  • &'=""
Note: Match FAILED (match variables are undefined).
19. /\bthe\b/
    "It is full of Python heritage that shall never wither in time; therefore, 'At Last the 1948 Show' is one of my favorite shows."
  • $`="It is full of Python heritage that shall never wither in time; therefore, 'At Last "
  • $&="the"
  • &'=" 1948 Show' is one of my favorite shows."
20. /the\b/
    "It is full of Python heritage that shall never wither in time; therefore, 'At Last the 1948 Show' is one of my favorite shows."
  • $`="It is full of Python heritage that shall never wither in time; therefore, 'At Last "
  • $&="the"
  • &'=" 1948 Show' is one of my favorite shows."
21. /\bthe/
    "It is full of Python heritage that shall never wither in time; therefore, 'At Last the 1948 Show' is one of my favorite shows."
  • $`="It is full of Python heritage that shall never wither in time; "
  • $&="the"
  • &'="refore, 'At Last the 1948 Show' is one of my favorite shows."
Back to menu (top)