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
There are currently no sponsors. Become a sponsor today!
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
  • Non-capturing group
    (?:...)
  • Capturing group
    (...)
  • 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

%
%
mg

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"^\D{8}(?<severity>\w)(?<time>\d{4}\s[^\s]*)\s+(?<pid>\d+)\s+(?<source>[^ \]]+)\](?<message>.*[/\/n])([\W]*[\w]*){3}[\W]{3}(?<logdate>[\d]*\-[\d]*\-[\d]*)\w(?<logtime>[\d]*\:[\d]*\:[\d]*\.[\d]*)\w\W*$" test_str = ("{\"log\":\"I1219 13:32:01.389913 1 server.go:114] Version: v1.8.0\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:01.390360472Z\"}\n" "{\"log\":\"I1219 13:32:01.390379 1 plugins.go:101] No cloud provider specified.\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:01.390969576Z\"}\n" "{\"log\":\"I1219 13:32:01.634232 1 feature_gate.go:156] feature gates: map[Initializers:true]\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:01.635347611Z\"}\n" "{\"log\":\"I1219 13:32:01.635456 1 initialization.go:84] enabled Initializers feature as part of admission plugin setup\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:01.635590586Z\"}\n" "{\"log\":\"W1219 13:32:01.636255 1 admission.go:66] PersistentVolumeLabel admission controller is deprecated. Please remove this controller from your configuration files and scripts.\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:01.636854971Z\"}\n" "{\"log\":\"W1219 13:32:01.713333 1 genericapiserver.go:317] Skipping API batch/v2alpha1 because it has no resources.\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:01.713583761Z\"}\n" "{\"log\":\"W1219 13:32:01.731298 1 genericapiserver.go:317] Skipping API rbac.authorization.k8s.io/v1alpha1 because it has no resources.\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:01.732776091Z\"}\n" "{\"log\":\"[restful] 2017/12/19 13:32:01 log.go:33: [restful/swagger] listing is available at https://185.150.9.204:6443/swaggerapi\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:01.799479979Z\"}\n" "{\"log\":\"[restful] 2017/12/19 13:32:01 log.go:33: [restful/swagger] https://185.150.9.204:6443/swaggerui/ is mapped to folder /swagger-ui/\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:01.799497746Z\"}\n" "{\"log\":\"[restful] 2017/12/19 13:32:02 log.go:33: [restful/swagger] listing is available at https://185.150.9.204:6443/swaggerapi\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:02.471721558Z\"}\n" "{\"log\":\"[restful] 2017/12/19 13:32:02 log.go:33: [restful/swagger] https://185.150.9.204:6443/swaggerui/ is mapped to folder /swagger-ui/\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:02.47174796Z\"}\n" "{\"log\":\"I1219 13:32:04.992367 1 serve.go:85] Serving securely on 0.0.0.0:6443\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:04.993893788Z\"}\n" "{\"log\":\"I1219 13:32:04.992843 1 apiservice_controller.go:112] Starting APIServiceRegistrationController\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:04.993922573Z\"}\n" "{\"log\":\"I1219 13:32:04.992865 1 cache.go:32] Waiting for caches to sync for APIServiceRegistrationController controller\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:04.993925574Z\"}\n" "{\"log\":\"I1219 13:32:04.992909 1 available_controller.go:192] Starting AvailableConditionController\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:04.993927705Z\"}\n" "{\"log\":\"I1219 13:32:04.992925 1 cache.go:32] Waiting for caches to sync for AvailableConditionController controller\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:04.993929704Z\"}\n" "{\"log\":\"I1219 13:32:04.992944 1 crd_finalizer.go:242] Starting CRDFinalizer\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:04.993931736Z\"}\n" "{\"log\":\"I1219 13:32:04.992949 1 customresource_discovery_controller.go:152] Starting DiscoveryController\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:04.993933463Z\"}\n" "{\"log\":\"I1219 13:32:04.992953 1 naming_controller.go:277] Starting NamingConditionController\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:04.993935376Z\"}\n" "{\"log\":\"I1219 13:32:04.992975 1 crdregistration_controller.go:112] Starting crd-autoregister controller\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:04.993937164Z\"}\n" "{\"log\":\"I1219 13:32:04.992979 1 controller_utils.go:1041] Waiting for caches to sync for crd-autoregister controller\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:04.993939029Z\"}\n" "{\"log\":\"I1219 13:32:04.994147 1 controller.go:84] Starting OpenAPI AggregationController\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:04.994289148Z\"}\n" "{\"log\":\"I1219 13:32:05.092979 1 cache.go:39] Caches are synced for APIServiceRegistrationController controller\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:05.093392359Z\"}\n" "{\"log\":\"I1219 13:32:05.092978 1 cache.go:39] Caches are synced for AvailableConditionController controller\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:05.093418406Z\"}\n" "{\"log\":\"I1219 13:32:05.093102 1 controller_utils.go:1048] Caches are synced for crd-autoregister controller\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:05.093430972Z\"}\n" "{\"log\":\"I1219 13:32:05.093142 1 autoregister_controller.go:136] Starting autoregister controller\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:05.093433199Z\"}\n" "{\"log\":\"I1219 13:32:05.093146 1 cache.go:32] Waiting for caches to sync for autoregister controller\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:05.093435176Z\"}\n" "{\"log\":\"I1219 13:32:05.193207 1 cache.go:39] Caches are synced for autoregister controller\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:05.193541587Z\"}\n" "{\"log\":\"I1219 13:32:09.998168 1 trace.go:76] Trace[1836345649]: \\\"Create /api/v1/namespaces\\\" (started: 2017-12-19 13:32:05.995599565 +0000 UTC) (total time: 4.002539088s):\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:10.008954395Z\"}\n" "{\"log\":\"Trace[1836345649]: [4.000446951s] [4.000418303s] About to store object in database\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:10.008974063Z\"}\n" "{\"log\":\"I1219 13:32:10.629009 1 trace.go:76] Trace[1988456937]: \\\"Create /api/v1/namespaces/kube-system/events\\\" (started: 2017-12-19 13:32:06.625541932 +0000 UTC) (total time: 4.003444046s):\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:10.63530296Z\"}\n" "{\"log\":\"Trace[1988456937]: [4.001682236s] [4.001642828s] About to store object in database\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:10.635326659Z\"}\n" "{\"log\":\"I1219 13:32:11.593994 1 trace.go:76] Trace[1945366238]: \\\"Create /api/v1/nodes\\\" (started: 2017-12-19 13:32:07.591993863 +0000 UTC) (total time: 4.00197912s):\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:11.59759386Z\"}\n" "{\"log\":\"Trace[1945366238]: [4.000615689s] [4.000505107s] About to store object in database\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:11.597618565Z\"}\n" "{\"log\":\"I1219 13:32:12.651139 1 trace.go:76] Trace[648564056]: \\\"Create /api/v1/namespaces/kube-system/events\\\" (started: 2017-12-19 13:32:08.641834265 +0000 UTC) (total time: 4.009285942s):\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:12.65281182Z\"}\n" "{\"log\":\"Trace[648564056]: [4.001050787s] [4.001021095s] About to store object in database\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:12.652826332Z\"}\n" "{\"log\":\"I1219 13:32:14.684575 1 trace.go:76] Trace[1339119512]: \\\"Create /apis/authentication.k8s.io/v1/tokenreviews\\\" (started: 2017-12-19 13:32:10.677360533 +0000 UTC) (total time: 4.007189394s):\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:14.689524885Z\"}\n" "{\"log\":\"Trace[1339119512]: [4.004918437s] [4.004846143s] About to store object in database\\n\",\"stream\":\"stderr\",\"time\":\"2017-12-19T13:32:14.689537857Z\"}\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