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

Perl Regex Examples 02:


back to Perl REGEX reference overview page
Easy REGEX
More examples here.
# REGEX Pattern:
    Bound String that is tested...
  • Pre-Match Region = $`=""
  • Match Region = $&=""
  • Post-Match Region = &'=""
  • Parenthetical Capture Groups ($1 through $9) = ...
01. /,.*,/
    "829.858550368314, 237.948537643572, 873.3182967052, 379.721359231528"
  • $`="829.858550368314"
  • $&=", 237.948537643572, 873.3182967052,"
  • &'=" 379.721359231528"
  • $1=undef
  • $2=undef
  • $3=undef
  • $4=undef
  • $5=undef
  • $6=undef
  • $7=undef
  • $8=undef
  • $9=undef
02. /,\.*,/
    "829.858550368314, 237.948537643572, 873.3182967052, 379.721359231528"
  • $`=undef
  • $&=undef
  • &'=undef
  • $1=undef
  • $2=undef
  • $3=undef
  • $4=undef
  • $5=undef
  • $6=undef
  • $7=undef
  • $8=undef
  • $9=undef
03. /.*,\s(.*)/
    "829.858550368314, 237.948537643572, 873.3182967052, 379.721359231528"
  • $`=""
  • $&="829.858550368314, 237.948537643572, 873.3182967052, 379.721359231528"
  • &'=""
  • $1="379.721359231528"
  • $2=undef
  • $3=undef
  • $4=undef
  • $5=undef
  • $6=undef
  • $7=undef
  • $8=undef
  • $9=undef
04. /.*,\s+(.*)/
    "829.858550368314, 237.948537643572, 873.3182967052, 379.721359231528""
  • $`=""
  • $&="829.858550368314, 237.948537643572, 873.3182967052, 379.721359231528"
  • &'=""
  • $1="379.721359231528"
  • $2=undef
  • $3=undef
  • $4=undef
  • $5=undef
  • $6=undef
  • $7=undef
  • $8=undef
  • $9=undef
05. /.*,\s*(.*)/
    "829.858550368314, 237.948537643572, 873.3182967052, 379.721359231528""
  • $`=""
  • $&="829.858550368314, 237.948537643572, 873.3182967052, 379.721359231528"
  • &'=""
  • $1="379.721359231528"
  • $2=undef
  • $3=undef
  • $4=undef
  • $5=undef
  • $6=undef
  • $7=undef
  • $8=undef
  • $9=undef
06. /([^,]+),.*/
    "829.858550368314, 237.948537643572, 873.3182967052, 379.721359231528""
  • $`=""
  • $&="829.858550368314, 237.948537643572, 873.3182967052, 379.721359231528"
  • &'=""
  • $1="829.858550368314"
  • $2=undef
  • $3=undef
  • $4=undef
  • $5=undef
  • $6=undef
  • $7=undef
  • $8=undef
  • $9=undef
07. /([^,]+).*/
    "829.858550368314, 237.948537643572, 873.3182967052, 379.721359231528""
  • $`=""
  • $&="829.858550368314, 237.948537643572, 873.3182967052, 379.721359231528"
  • &'=""
  • $1="829.858550368314"
  • $2=undef
  • $3=undef
  • $4=undef
  • $5=undef
  • $6=undef
  • $7=undef
  • $8=undef
  • $9=undef

Medium REGEX
# REGEX Pattern:
    Bound String that is tested...
  • Pre-Match Region = $`=""
  • Match Region = $&=""
  • Post-Match Region = &'=""
  • Parenthetical Capture Groups ($1 through $9) = ...
01. /(\w+)\s+(\d+)\s+([\d:]+)/
    "Oct 4 23:09:26 ox3 sshd[42]: Failed password for invalid user root from 143.229.53.123"
  • $`=""
  • $&="Oct 4 23:09:26"
  • &'=" ox3 sshd[42]: Failed password for invalid user root from 143.229.53.123"
  • $1="Oct"
  • $2="4"
  • $3="23:09:26"
  • $4=undef
  • $5=undef
  • $6=undef
  • $7=undef
  • $8=undef
  • $9=undef
02. /([\d|\.]+)/
    "Oct 4 23:09:26 ox3 sshd[42]: Failed password for invalid user root from 143.229.53.123"
  • $`="Oct "
  • $&="4"
  • &'=" 23:09:26 ox3 sshd[42]: Failed password for invalid user root from 143.229.53.123"
  • $1="4"
  • $2=undef
  • $3=undef
  • $4=undef
  • $5=undef
  • $6=undef
  • $7=undef
  • $8=undef
  • $9=undef
03. /from([\d|\.]+)/
    "Oct 4 23:09:26 ox3 sshd[42]: Failed password for invalid user root from 143.229.53.123"
  • $`=undef
  • $&=undef
  • &'=undef
  • $1=undef
  • $2=undef
  • $3=undef
  • $4=undef
  • $5=undef
  • $6=undef
  • $7=undef
  • $8=undef
  • $9=undef
04. /from\s([\d|\.]+)/
    "Oct 4 23:09:26 ox3 sshd[42]: Failed password for invalid user root from 143.229.53.123"
  • $`="Oct 4 23:09:26 ox3 sshd[42]: Failed password for invalid user root "
  • $&="from 143.229.53.123"
  • &'=""
  • $1="143.229.53.123"
  • $2=undef
  • $3=undef
  • $4=undef
  • $5=undef
  • $6=undef
  • $7=undef
  • $8=undef
  • $9=undef
05. /([\d|\.]+)$/
    "Oct 4 23:09:26 ox3 sshd[42]: Failed password for invalid user root from 143.229.53.123"
  • $`="Oct 4 23:09:26 ox3 sshd[42]: Failed password for invalid user root from "
  • $&=""
  • &'="143.229.53.123"
  • $1="143.229.53.123"
  • $2=undef
  • $3=undef
  • $4=undef
  • $5=undef
  • $6=undef
  • $7=undef
  • $8=undef
  • $9=undef
06. /(\S+)$/
    "Oct 4 23:09:26 ox3 sshd[42]: Failed password for invalid user root from 143.229.53.123"
  • $`="Oct 4 23:09:26 ox3 sshd[42]: Failed password for invalid user root from "
  • $&="143.229.53.123"
  • &'=""
  • $1="143.229.53.123"
  • $2=undef
  • $3=undef
  • $4=undef
  • $5=undef
  • $6=undef
  • $7=undef
  • $8=undef
  • $9=undef
07. /((\d+)\.)+(\d+)/
    Warning: Tricky one that does NOT capture all 4 IPv4 octets.

    "Oct 4 23:09:26 ox3 sshd[42]: Failed password for invalid user root from 143.229.53.123"
  • $`="Oct 4 23:09:26 ox3 sshd[42]: Failed password for invalid user root from "
  • $&="143.229.53.123"
  • &'=""
  • $1="53."
  • $2="53"
  • $3="123"
  • $4=undef
  • $5=undef
  • $6=undef
  • $7=undef
  • $8=undef
  • $9=undef
08. /(\d+)\.(\d+)\.(\d+)\.(\d+)/
    "Oct 4 23:09:26 ox3 sshd[42]: Failed password for invalid user root from 143.229.53.123"
  • $`="Oct 4 23:09:26 ox3 sshd[42]: Failed password for invalid user root from "
  • $&="143.229.53.123"
  • &'=""
  • $1="143"
  • $2="229"
  • $3="53"
  • $4="123"
  • $5=undef
  • $6=undef
  • $7=undef
  • $8=undef
  • $9=undef
09. /for\sinvalid\suser\s(.*)\sfrom/
    "Oct 4 23:09:26 ox3 sshd[42]: Failed password for invalid user root from 143.229.53.123"
  • $`="Oct 4 23:09:26 ox3 sshd[42]: Failed password "
  • $&="for invalid user root from"
  • &'=" 143.229.53.123"
  • $1="root"
  • $2=undef
  • $3=undef
  • $4=undef
  • $5=undef
  • $6=undef
  • $7=undef
  • $8=undef
  • $9=undef
10. /ox3\s(\w+)\[(\d+)\]:/
    "Oct 4 23:09:26 ox3 sshd[42]: Failed password for invalid user root from 143.229.53.123"
  • $`="Oct 4 23:09:26 "
  • $&="ox3 sshd[42]:"
  • &'=" Failed password for invalid user root from 143.229.53.123"
  • $1="sshd"
  • $2="42"
  • $3=undef
  • $4=undef
  • $5=undef
  • $6=undef
  • $7=undef
  • $8=undef
  • $9=undef
11. /(.+)\s(.+)\s(\w+)\[\d+\]:.*user\s(\S+)\sfrom\s(.*)/
    "Oct 4 23:09:26 ox3 sshd[42]: Failed password for invalid user root from 143.229.53.123"
  • $`=""
  • $&="Oct 4 23:09:26 ox3 sshd[42]: Failed password for invalid user root from 143.229.53.123"
  • &'=""
  • $1="Oct 4 23:09:26"
  • $2="ox3"
  • $3="sshd"
  • $4="root"
  • $5="143.229.53.123"
  • $6=undef
  • $7=undef
  • $8=undef
  • $9=undef
12. /([^,]+)(\s*,\s*)(\1)\2(\1)\2(\1)/
    Warning: Tricky one that does NOT capture using backreferences (see next one).

    "829.858550368314, 237.948537643572, 873.3182967052, 379.721359231528"
  • $`=undef
  • $&=undef
  • &'=undef
  • $1=undef
  • $2=undef
  • $3=undef
  • $4=undef
  • $5=undef
  • $6=undef
  • $7=undef
  • $8=undef
  • $9=undef
13. /([^,]+)(\s*,\s*)([^,]+)\2([^,]+)\2([^,]+)/
    Warning: Understand why the last one did not work with backreferences.
    Realize that if your CSV columns have different white-spacing between each col
    (from each other), then you wouldn't be able to backreference this way.


    "829.858550368314, 237.948537643572, 873.3182967052, 379.721359231528"
  • $`=""
  • $&="829.858550368314, 237.948537643572, 873.3182967052, 379.721359231528"
  • &'=""
  • $1="829.858550368314"
  • $2=", "
  • $3="237.948537643572"
  • $4="873.3182967052"
  • $5="379.721359231528"
  • $6=undef
  • $7=undef
  • $8=undef
  • $9=undef
14. /([^,]+)(\s*,\s*)([^,]+)\2([^,]+)\2([^,]+)/
    Warning: The string has changed from the last one to exhibit backreferencing issues.
    Be careful that you fully understand backreferencing.
    This one worked to backreference the way it was written;
    however, notice that spaces were caught in with the numbers.
    Realize that if your CSV columns have different white-spacing between each col
    (from each other), then you wouldn't be able to backreference this way.


    "829.858550368314 , 237.948537643572 ,873.3182967052, 379.721359231528"
  • $`=""
  • $&="829.858550368314, 237.948537643572, 873.3182967052, 379.721359231528"
  • &'=""
  • $1="829.858550368314 "
  • $2=","
  • $3=" 237.948537643572 "
  • $4="873.3182967052"
  • $5=" 379.721359231528"
  • $6=undef
  • $7=undef
  • $8=undef
  • $9=undef

Difficult REGEX
# REGEX Pattern:
    Bound String that is tested...
  • Pre-Match Region = $`=""
  • Match Region = $&=""
  • Post-Match Region = &'=""
  • Parenthetical Capture Groups ($1 through $9) = ...
01. //
    "Oct 4 23:09:26 ox3 sshd[42]: Failed password for invalid user root from 143.229.53.123"
  • $`=""
  • $&=""
  • &'=""