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

$re = '%^\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*$%m'; $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"} {"log":"I1219 13:32:01.390379 1 plugins.go:101] No cloud provider specified.\\n","stream":"stderr","time":"2017-12-19T13:32:01.390969576Z"} {"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"} {"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"} {"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"} {"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"} {"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"} {"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"} {"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"} {"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"} {"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"} {"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"} {"log":"I1219 13:32:04.992843 1 apiservice_controller.go:112] Starting APIServiceRegistrationController\\n","stream":"stderr","time":"2017-12-19T13:32:04.993922573Z"} {"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"} {"log":"I1219 13:32:04.992909 1 available_controller.go:192] Starting AvailableConditionController\\n","stream":"stderr","time":"2017-12-19T13:32:04.993927705Z"} {"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"} {"log":"I1219 13:32:04.992944 1 crd_finalizer.go:242] Starting CRDFinalizer\\n","stream":"stderr","time":"2017-12-19T13:32:04.993931736Z"} {"log":"I1219 13:32:04.992949 1 customresource_discovery_controller.go:152] Starting DiscoveryController\\n","stream":"stderr","time":"2017-12-19T13:32:04.993933463Z"} {"log":"I1219 13:32:04.992953 1 naming_controller.go:277] Starting NamingConditionController\\n","stream":"stderr","time":"2017-12-19T13:32:04.993935376Z"} {"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"} {"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"} {"log":"I1219 13:32:04.994147 1 controller.go:84] Starting OpenAPI AggregationController\\n","stream":"stderr","time":"2017-12-19T13:32:04.994289148Z"} {"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"} {"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"} {"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"} {"log":"I1219 13:32:05.093142 1 autoregister_controller.go:136] Starting autoregister controller\\n","stream":"stderr","time":"2017-12-19T13:32:05.093433199Z"} {"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"} {"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"} {"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"} {"log":"Trace[1836345649]: [4.000446951s] [4.000418303s] About to store object in database\\n","stream":"stderr","time":"2017-12-19T13:32:10.008974063Z"} {"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"} {"log":"Trace[1988456937]: [4.001682236s] [4.001642828s] About to store object in database\\n","stream":"stderr","time":"2017-12-19T13:32:10.635326659Z"} {"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"} {"log":"Trace[1945366238]: [4.000615689s] [4.000505107s] About to store object in database\\n","stream":"stderr","time":"2017-12-19T13:32:11.597618565Z"} {"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"} {"log":"Trace[648564056]: [4.001050787s] [4.001021095s] About to store object in database\\n","stream":"stderr","time":"2017-12-19T13:32:12.652826332Z"} {"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"} {"log":"Trace[1339119512]: [4.004918437s] [4.004846143s] About to store object in database\\n","stream":"stderr","time":"2017-12-19T13:32:14.689537857Z"} '; preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0); // Print the entire match result var_dump($matches);

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 PHP, please visit: http://php.net/manual/en/ref.pcre.php