Regular Expressions 101

Save & Share

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java 8
  • .NET 7.0 (C#)
  • Rust
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests

Tools

Sponsors
An explanation of your regex will be automatically generated as you type.
Detailed match information will be displayed here automatically.
  • All Tokens
  • Common Tokens
  • General Tokens
  • Anchors
  • Meta Sequences
  • Quantifiers
  • Group Constructs
  • Character Classes
  • Flags/Modifiers
  • Substitution
  • A single character of: a, b or c
    [abc]
  • A character except: a, b or c
    [^abc]
  • A character in the range: a-z
    [a-z]
  • A character not in the range: a-z
    [^a-z]
  • A character in the range: a-z or A-Z
    [a-zA-Z]
  • Any single character
    .
  • Alternate - match either a or b
    a|b
  • Any whitespace character
    \s
  • Any non-whitespace character
    \S
  • Any digit
    \d
  • Any non-digit
    \D
  • Any word character
    \w
  • Any non-word character
    \W
  • Match everything enclosed
    (?:...)
  • Capture everything enclosed
    (...)
  • Zero or one of a
    a?
  • Zero or more of a
    a*
  • One or more of a
    a+
  • Exactly 3 of a
    a{3}
  • 3 or more of a
    a{3,}
  • Between 3 and 6 of a
    a{3,6}
  • Start of string
    ^
  • End of string
    $
  • A word boundary
    \b
  • Non-word boundary
    \B

Regular Expression
No Match

r"
"
gm

Test String

Code Generator

Generated Code

# coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility import re regex = r"[a-zA-Z]+\s\s\d|(2[0-3]|[01]?[0-9]):([0-5]?[0-9]):([0-5]?[0-9])|([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})|(2[0-3]|[01]?[0-9]):([0-5]?[0-9]):([0-5]?[0-9])|0[1-9]|[12][0-9]|3[01][-/.](0[1-9]|1[012])[-/.](19|20)" test_str = ("May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-cind-mbbkulfm2uns05 MBBKULFM2UNS05 - - - NetScreen device_id=MBBKULFM2UNS05 [Root]system-notification-00257(traffic): start_time=\"2017-05-04 14:37:01\" duration=0 policy_id=13 service=https proto=6 src zone=Untrust dst zone=Trust action=Permit sent=0 rcvd=0 src=23.15.10.113 dst=121.120.246.39 src_port=62160 dst_port=443 src-xlated ip=23.15.10.113 port=62160 dst-xlated ip=121.120.246.39 port=443 session_id=504659 reason=Creation 2017-05-04T14:37:26.000+0800 Permit 14 4 37 may 26 thursday 2017 local MBBKULFM2UNS05 121.120.246.39 443 0 10.1.24.91 main 23.15.10.113 1 13 62160 6 ___::_...__--::+:_--__-_-_-__=__[]--():_=\"--_::\"_= 0 Creation 0 https 504659 udp:514 log_management indexer_kldc5 23.15.10.113 62160 4/5/2017 14:37 16 0 Untrust\n" " \n" "May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-biz02-mbbkulfm2uns02 mbbkulfm2uns02 - - - NetScreen device_id=mbbkulfm2uns02 [Root]system-notification-00257(traffic): start_time=\"2017-05-04 14:37:00\" duration=0 policy_id=324 service=https proto=6 src zone=Untrust dst zone=Trust action=Permit sent=0 rcvd=0 src=113.210.200.243 dst=202.162.17.118 src_port=40558 dst_port=443 src-xlated ip=113.210.200.243 port=40558 dst-xlated ip=202.162.17.118 port=443 session_id=297570 reason=Creation 2017-05-04T14:37:26.000+0800 Permit 14 4 37 may 26 thursday 2017 local mbbkulfm2uns02 202.162.17.118 443 0 10.1.24.91 main 113.210.200.243 1 324 40558 6 ___::_...__--::+:_--__-_-_-__=__[]--():_=\"--_::\"_= 0 Creation 0 https 297570 udp:514 log_management indexer_kldc5 113.210.200.243 40558 4/5/2017 14:37 16 0 Untrust\n" " \n" "May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-myloca-drm2x01 MBBKULFMLCGW01 - - - NetScreen device_id=MBBKULFMLCGW01 [Root]system-notification-00257(traffic): start_time=\"2017-05-04 14:37:01\" duration=0 policy_id=5 service=https proto=6 src zone=V1-Untrust dst zone=V1-Trust action=Permit sent=0 rcvd=0 src=203.106.85.190 dst=203.153.92.65 src_port=48946 dst_port=443 src-xlated ip=203.106.85.190 port=48946 dst-xlated ip=203.153.92.65 port=443 session_id=249013 reason=Creation 2017-05-04T14:37:26.000+0800 Permit 14 4 37 may 26 thursday 2017 local MBBKULFMLCGW01 203.153.92.65 443 0 10.1.24.91 main 203.106.85.190 1 5 48946 6 ___::_...__--::+:_--__-_-_-__=__[]--():_=\"--_::\"_= 0 Creation 0 https 249013 udp:514 log_management indexer_kldc5 203.106.85.190 48946 4/5/2017 14:37 16 0 V1-Untrust\n" " \n" "May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-nonbiz01-mbbkulfm2uns03 MBBKULFM2UNS03 - - - NetScreen device_id=MBBKULFM2UNS03 [Root]system-notification-00257(traffic): start_time=\"2017-05-04 14:37:02\" duration=0 policy_id=560 service=https proto=6 src zone=V1-Trust dst zone=V1-Untrust action=Permit sent=0 rcvd=0 src=202.162.18.66 dst=52.6.70.163 src_port=39512 dst_port=443 src-xlated ip=202.162.18.66 port=39512 dst-xlated ip=52.6.70.163 port=443 session_id=208829 reason=Creation 2017-05-04T14:37:26.000+0800 Permit 14 4 37 may 26 thursday 2017 local MBBKULFM2UNS03 52.6.70.163 443 0 10.1.24.91 main 202.162.18.66 1 560 39512 6 ___::_...__--::+:_--__-_-_-__=__[]--():_=\"--_::\"_= 0 Creation 0 https 208829 udp:514 log_management indexer_kldc5 202.162.18.66 39512 4/5/2017 14:37 16 0 V1-Trust\n" " \n" "May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-nonbiz01-mbbkulfm2uns03 MBBKULFM2UNS03 - - - NetScreen device_id=MBBKULFM2UNS03 [Root]system-notification-00257(traffic): start_time=\"2017-05-04 14:37:02\" duration=0 policy_id=574 service=udp/port:15000 proto=17 src zone=V1-Untrust dst zone=V1-Trust action=Deny sent=0 rcvd=125 src=8.8.8.8 dst=202.162.18.190 src_port=53 dst_port=15000 session_id=0 2017-05-04T14:37:26.000+0800 Deny 14 4 37 may 26 thursday 2017 local MBBKULFM2UNS03 202.162.18.190 15000 0 10.1.24.91 main 1 574 17 ___::_...__--::+:_--__-_-_-__=__[]--():_=\"--_::\"_= 125 0 udp/port:15000 0 udp:514 log_management indexer_kldc5 8.8.8.8 53 4/5/2017 14:37 16 0 V1-Untrust\n" " \n" "May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-nonbiz01-mbbkulfm2uns03 MBBKULFM2UNS03 - - - NetScreen device_id=MBBKULFM2UNS03 [Root]system-notification-00257(traffic): start_time=\"2017-05-04 14:37:02\" duration=0 policy_id=560 service=dns proto=17 src zone=V1-Trust dst zone=V1-Untrust action=Permit sent=0 rcvd=0 src=202.162.18.130 dst=8.8.8.8 src_port=26311 dst_port=53 src-xlated ip=202.162.18.130 port=26311 dst-xlated ip=8.8.8.8 port=53 session_id=185596 reason=Creation 2017-05-04T14:37:26.000+0800 Permit 14 4 37 may 26 thursday 2017 local MBBKULFM2UNS03 8.8.8.8 53 0 10.1.24.91 main 202.162.18.130 1 560 26311 17 ___::_...__--::+:_--__-_-_-__=__[]--():_=\"--_::\"_= 0 Creation 0 dns 185596 udp:514 log_management indexer_kldc5 202.162.18.130 26311 4/5/2017 14:37 16 0 V1-Trust\n" " \n" "May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-nonbiz01-mbbkulfm2uns03 MBBKULFM2UNS03 - - - NetScreen device_id=MBBKULFM2UNS03 [Root]system-notification-00257(traffic): start_time=\"2017-05-04 14:37:02\" duration=0 policy_id=560 service=https proto=6 src zone=V1-Trust dst zone=V1-Untrust action=Permit sent=0 rcvd=0 src=202.162.18.66 dst=52.6.70.163 src_port=39511 dst_port=443 src-xlated ip=202.162.18.66 port=39511 dst-xlated ip=52.6.70.163 port=443 session_id=200606 reason=Creation 2017-05-04T14:37:26.000+0800 Permit 14 4 37 may 26 thursday 2017 local MBBKULFM2UNS03 52.6.70.163 443 0 10.1.24.91 main 202.162.18.66 1 560 39511 6 ___::_...__--::+:_--__-_-_-__=__[]--():_=\"--_::\"_= 0 Creation 0 https 200606 udp:514 log_management indexer_kldc5 202.162.18.66 39511 4/5/2017 14:37 16 0 V1-Trust\n" " \n" "May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-biz02-mbbkulfm2uns02 mbbkulfm2uns02 - - - NetScreen device_id=mbbkulfm2uns02 [Root]system-notification-00257(traffic): start_time=\"2017-05-04 14:37:00\" duration=0 policy_id=270 service=https proto=6 src zone=Untrust dst zone=Trust action=Permit sent=0 rcvd=0 src=202.162.17.210 dst=202.162.17.122 src_port=41814 dst_port=443 src-xlated ip=202.162.17.210 port=41814 dst-xlated ip=202.162.17.122 port=443 session_id=391371 reason=Creation 2017-05-04T14:37:26.000+0800 Permit 14 4 37 may 26 thursday 2017 local mbbkulfm2uns02 202.162.17.122 443 0 10.1.24.91 main 202.162.17.210 1 270 41814 6 ___::_...__--::+:_--__-_-_-__=__[]--():_=\"--_::\"_= 0 Creation 0 https 391371 udp:514 log_management indexer_kldc5 202.162.17.210 41814 4/5/2017 14:37 16 0 Untrust\n" " \n" "May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-cind-mbbkulfm2uns05 MBBKULFM2UNS05 - - - NetScreen device_id=MBBKULFM2UNS05 [Root]system-notification-00257(traffic): start_time=\"2017-05-04 14:37:01\" duration=0 policy_id=13 service=https proto=6 src zone=Untrust dst zone=Trust action=Permit sent=0 rcvd=0 src=203.106.85.197 dst=121.120.246.39 src_port=50539 dst_port=443 src-xlated ip=203.106.85.197 port=50539 dst-xlated ip=121.120.246.39 port=443 session_id=504075 reason=Creation 2017-05-04T14:37:26.000+0800 Permit 14 4 37 may 26 thursday 2017 local MBBKULFM2UNS05 121.120.246.39 443 0 10.1.24.91 main 203.106.85.197 1 13 50539 6 ___::_...__--::+:_--__-_-_-__=__[]--():_=\"--_::\"_= 0 Creation 0 https 504075 udp:514 log_management indexer_kldc5 203.106.85.197 50539 4/5/2017 14:37 16 0 Untrust\n" " \n" "May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-biz02-mbbkulfm2uns02 mbbkulfm2uns02 - - - NetScreen device_id=mbbkulfm2uns02 [Root]system-notification-00257(traffic): start_time=\"2017-05-04 14:37:00\" duration=0 policy_id=2 service=icmp proto=1 src zone=Global dst zone=Global action=Permit sent=0 rcvd=0 src=202.162.17.210 dst=202.162.17.119 icmp type=8 icmp code=0 src-xlated ip=202.162.17.210 dst-xlated ip=202.162.17.119 session_id=400977 reason=Creation 2017-05-04T14:37:26.000+0800 Permit 0 14 4 37 may 26 thursday 2017 local mbbkulfm2uns02 202.162.17.119 0 10.1.24.91 main 202.162.17.210 1 2 1 ___::_...__--::+:_--__-_-_-__=__[]--():_=\"--_::\"_= 0 Creation 0 icmp 400977 udp:514 log_management indexer_kldc5 202.162.17.210 4/5/2017 14:37 16 0 8 Global\n" " \n" "May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-cind-mbbkulfm2uns05 MBBKULFM2UNS05 - - - NetScreen device_id=MBBKULFM2UNS05 [Root]system-notification-00257(traffic): start_time=\"2017-05-04 14:37:01\" duration=0 policy_id=13 service=https proto=6 src zone=Untrust dst zone=Trust action=Permit sent=0 rcvd=0 src=203.106.85.20 dst=121.120.246.39 src_port=54543 dst_port=443 src-xlated ip=203.106.85.20 port=54543 dst-xlated ip=121.120.246.39 port=443 session_id=519071 reason=Creation 2017-05-04T14:37:26.000+0800 Permit 14 4 37 may 26 thursday 2017 local MBBKULFM2UNS05 121.120.246.39 443 0 10.1.24.91 main 203.106.85.20 1 13 54543 6 ___::_...__--::+:_--__-_-_-__=__[]--():_=\"--_::\"_= 0 Creation 0 https 519071 udp:514 log_management indexer_kldc5 203.106.85.20 54543 4/5/2017 14:37 16 0 Untrust\n" " \n" "May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-nonbiz01-mbbkulfm2uns03 MBBKULFM2UNS03 - - - NetScreen device_id=MBBKULFM2UNS03 [Root]system-notification-00257(traffic): start_time=\"2017-05-04 14:37:02\" duration=0 policy_id=445 service=dns proto=17 src zone=V1-Trust dst zone=V1-Untrust action=Permit sent=0 rcvd=0 src=202.162.18.191 dst=8.8.4.4 src_port=59640 dst_port=53 src-xlated ip=202.162.18.191 port=59640 dst-xlated ip=8.8.4.4 port=53 session_id=207747 reason=Creation 2017-05-04T14:37:26.000+0800 Permit 14 4 37 may 26 thursday 2017 local MBBKULFM2UNS03 8.8.4.4 53 0 10.1.24.91 main 202.162.18.191 1 445 59640 17 ___::_...__--::+:_--__-_-_-__=__[]--():_=\"--_::\"_= 0 Creation 0 dns 207747 udp:514 log_management indexer_kldc5 202.162.18.191 59640 4/5/2017 14:37 16 0 V1-Trust\n" " \n" "May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-biz02-mbbkulfm2uns02 mbbkulfm2uns02 - - - NetScreen device_id=mbbkulfm2uns02 [Root]system-notification-00257(traffic): start_time=\"2017-05-04 14:37:00\" duration=0 policy_id=109 service=https proto=6 src zone=Untrust dst zone=Trust action=Permit sent=0 rcvd=0 src=14.1.225.89 dst=202.162.17.152 src_port=58279 dst_port=443 src-xlated ip=14.1.225.89 port=58279 dst-xlated ip=202.162.17.152 port=443 session_id=329728 reason=Creation 2017-05-04T14:37:26.000+0800 Permit 14 4 37 may 26 thursday 2017 local mbbkulfm2uns02 202.162.17.152 443 0 10.1.24.91 main 14.1.225.89 1 109 58279 6 ___::_...__--::+:_--__-_-_-__=__[]--():_=\"--_::\"_= 0 Creation 0 https 329728 udp:514 log_management indexer_kldc5 14.1.225.89 58279 4/5/2017 14:37 16 0 Untrust\n" " \n" "May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-cind-mbbkulfm2uns05 MBBKULFM2UNS05 - - - NetScreen device_id=MBBKULFM2UNS05 [Root]system-notification-00257(traffic): start_time=\"2017-05-04 14:37:01\" duration=0 policy_id=13 service=http proto=6 src zone=Untrust dst zone=Trust action=Permit sent=0 rcvd=0 src=23.11.200.142 dst=121.120.246.39 src_port=52451 dst_port=80 src-xlated ip=23.11.200.142 port=52451 dst-xlated ip=121.120.246.39 port=80 session_id=507541 reason=Creation 2017-05-04T14:37:26.000+0800 Permit 14 4 37 may 26 thursday 2017 local MBBKULFM2UNS05 121.120.246.39 80 0 10.1.24.91 main 23.11.200.142 1 13 52451 6 ___::_...__--::+:_--__-_-_-__=__[]--():_=\"--_::\"_= 0 Creation 0 http 507541 udp:514 log_management indexer_kldc5 23.11.200.142 52451 4/5/2017 14:37 16 0 Untrust\n" " \n" "May 4 14:37:26 10.1.24.91 1 2017-05-04T14:37:02+08:00 JUN-cind-mbbkulfm2uns05 MBBKULFM2UNS05 - - - NetScreen device_id=MBBKULFM2UNS05 [Root]system-notification-00257(traffic): start_time=\"2017-05-04 14:37:01\" duration=0 policy_id=13 service=https proto=6 src zone=Untrust dst zone=Trust action=Permit sent=0 rcvd=0 src=203.106.85.109 dst=121.120.246.39 src_port=36278 dst_port=443 src-xlated ip=203.106.85.109 port=36278 dst-xlated ip=121.120.246.39 port=443 session_id=518451 reason=Creation 2017-05-04T14:37:26.000+0800 Permit 14 4 37 may 26 thursday 2017 local MBBKULFM2UNS05 121.120.246.39 443 0 10.1.24.91 main 203.106.85.109 1 13 36278 6 ___::_...__--::+:_--__-_-_-__=__[]--():_=\"--_::\"_= 0 Creation 0 https 518451 udp:514 log_management indexer_kldc5 203.106.85.109 36278 4/5/2017 14:37 16 0 Untrust\n") matches = re.finditer(regex, test_str, re.MULTILINE) for matchNum, match in enumerate(matches, start=1): print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group())) for groupNum in range(0, len(match.groups())): groupNum = groupNum + 1 print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum))) # Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html