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
  • 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

/
/
mxg

Test String

Code Generator

Generated Code

$re = '/^.*((?<req_method>GET|POST|PUT|DELETE|CONNECT|OPTIONS|HEAD[^ ]\w+)\s*(?<req_path>[^ ][-._?=%&\/[:alnum:]]*)\s*(?<req_protocol>[^ ][.\/\dHTFSP]+){0,1})|(\s*(status:\s*(?<req_status>[^ ]\d+))\s*(len:\ (?<req_len>[^ ]\d+)){0,1}\s*(time:\s*(?<req_time>[^ ][.\d]+)){0,1}\s*(microversion:\s*(?<req_mver>[^ ][.\d]+)){0,1}){0,1}$ /mx'; $str = ' # suse 2017-08-14T12:25:56+00:00 d2c-76-8a-ac-64-50 nova-api: 0.3074s 10.60.2.84 GET /latest/user-data None:None 200 [Python-httplib2/0.9.2 (gzip)] text/plain text/plain 2017-08-14T12:25:56+00:00 d2c-76-8a-ac-64-50 nova-api: 10.3.4.171,10.60.2.84 "GET /latest/user-data HTTP/1.1" status: 200 len: 374 time: 0.0037200 2017-08-14T12:25:56+00:00 d2c-76-8a-ac-64-6e nova-api: 0.6576s 10.60.2.84 GET /latest/user-data None:None 200 [Python-httplib2/0.9.2 (gzip)] text/plain text/plain 2017-08-14T12:25:56+00:00 d2c-76-8a-ac-64-6e nova-api: 10.3.4.171,10.60.2.84 "GET /latest/user-data HTTP/1.1" status: 200 len: 374 time: 0.0074589 2017-08-14T12:25:56+00:00 d2c-76-8a-ac-65-68 nova-api: 0.2532s 10.60.2.84 GET /latest/user-data None:None 200 [Python-httplib2/0.9.2 (gzip)] text/plain text/plain 2017-08-14T12:25:56+00:00 d2c-76-8a-ac-65-68 nova-api: 10.3.4.172,10.60.2.84 "GET /latest/user-data HTTP/1.1" status: 200 len: 374 time: 0.0029602 kkk 2017-08-14T12:25:56+00:00 d2c-76-8a-ac-64-50 nova-api: 0.3033s 10.60.2.84 GET /latest/meta-data/public-keys/0/openssh-key None:None 200 [Python-httplib2/0.9.2 (gzip)] text/plain text/plain 2017-08-14T12:25:56+00:00 d2c-76-8a-ac-64-50 nova-api: 0.3033s 10.60.2.84 GET /latest/meta-data/public-keys/0/openssh-key None:None 200 [Python-httplib2/0.9.2 (gzip)] text/plain text/plain kkkk 2017-08-14T12:25:56+00:00 d2c-76-8a-ac-64-50 nova-api: 10.3.4.85,10.60.2.84 "GET /latest/meta-data/public-keys/0/openssh-key HTTP/1.1" status: 200 len: 881 time: 0.0036240 # mirantis ==> ./nova-api.log <== 2017-09-27 07:54:18.201 5490 INFO nova.metadata.wsgi.server [-] 172.17.45.11 "OPTIONS / HTTP/1.0" status: 200 len: 234 time: 0.0007069 2017-09-27 07:54:19.852 5490 INFO nova.metadata.wsgi.server [-] 172.17.45.13 "OPTIONS / HTTP/1.0" status: 200 len: 234 time: 0.0010118 2017-09-27 07:54:20.243 5474 INFO nova.osapi_compute.wsgi.server [req-88f8732a-4a36-42b0-9e00-6cf0aca395da 6335aa7b2294478d867b25f3b1b0c77c 26e4dd19485249608ee3685f254f3909 - default default] 172.17.48.232,172.17.45.13 "GET /v2.1/26e4dd19485249608ee3685f254f3909/servers/detail?name=%5Ectl03%5C.kkk-deploy-heat-k8s-ha-calico-161%5C.kkk-mk%5C.kk%24&status=ACTIVE HTTP/1.1" status: 200 len: 3239 time: 0.8562572 2017-09-27 07:54:21.291 5474 INFO nova.osapi_compute.wsgi.server [-] 172.17.45.12 "GET / HTTP/1.1" status: 200 len: 482 time: 0.0020030 2017-09-27 07:54:22.398 5474 INFO nova.osapi_compute.wsgi.server [-] 172.17.45.12 "OPTIONS / HTTP/1.0" status: 200 len: 501 time: 0.0011830 2017-09-27 07:54:23.323 5484 INFO nova.metadata.wsgi.server [-] 172.17.45.12 "OPTIONS / HTTP/1.0" status: 200 len: 234 time: 0.0010719 2017-09-27 07:54:25.713 5476 INFO nova.osapi_compute.wsgi.server [-] 172.17.45.13 "OPTIONS / HTTP/1.0" status: 200 len: 501 time: 0.0013640 2017-09-27 07:54:27.261 5473 INFO nova.osapi_compute.wsgi.server [-] 172.17.45.11 "OPTIONS / HTTP/1.0" status: 200 len: 501 time: 0.0017979 2017-09-27 07:54:27.594 5471 INFO nova.osapi_compute.wsgi.server [req-7ba73d12-c18a-4c59-9b35-95be06ea45a2 6335aa7b2294478d867b25f3b1b0c77c 26e4dd19485249608ee3685f254f3909 - default default] 172.17.48.231,172.17.45.13 "GET /v2.1/26e4dd19485249608ee3685f254f3909/servers/detail?name=%5Ectl01%5C.kkkk-deploy-heat-k8s-ha-calico-161%5C.kk-mk%5C.kk%24&status=ACTIVE HTTP/1.1" status: 200 len: 3239 time: 0.8599730 2017-09-27 07:54:27.885 5486 INFO nova.metadata.wsgi.server [-] 172.17.45.11 "OPTIONS / HTTP/1.0" status: 200 len: 234 time: 0.0009840 ==> ./nova-cert.log <== ==> ./nova-conductor.log <== File "/usr/lib/python2.7/dist-packages/nova/scheduler/manager.py", line 98, in select_destinations dests = self.driver.select_destinations(ctxt, spec_obj) File "/usr/lib/python2.7/dist-packages/nova/scheduler/filter_scheduler.py", line 79, in select_destinations raise exception.NoValidHost(reason=reason) NoValidHost: No valid host was found. There are not enough hosts available. 2017-09-27 07:15:40.310 21389 WARNING nova.scheduler.utils [req-c8be6870-ab24-4080-80e9-864130bf382a 38faea7239db49aca33e866451271d47 5e6af85573c5434190c59cbd2caa0558 - - -] [instance: 5a2e94e6-2dfa-4e15-b60c-d0819b456a3c] Setting instance to ERROR state. ==> ./nova-consoleauth.log <== ==> ./nova-manage.log <== ==> ./nova-novncproxy.log <== ==> ./nova-placement-api.log <== 2017-09-27 07:54:29.137 19423 INFO nova.api.openstack.placement.requestlog [req-b5049cf9-9453-4990-942d-1420bc91b33c 78061f04af2741309c970a4c360d1a27 2e6cdfb7870c43e8b5ed0d216430c0f2 - default default] 172.17.45.13 "GET /allocations/18fec513-0a0e-4427-9c9f-fecac9f2b3dd" status: 200 len: 139 microversion: 1.0 2017-09-27 07:54:29.255 19420 INFO nova.api.openstack.placement.requestlog [req-36804863-094b-415d-a7b4-ca60bdcba3cb 78061f04af2741309c970a4c360d1a27 2e6cdfb7870c43e8b5ed0d216430c0f2 - default default] 172.17.45.13 "GET /allocations/47ca5912-2f84-4619-8ea3-b13bba51e3ab" status: 200 len: 138 microversion: 1.0 2017-09-27 07:54:29.309 19423 INFO nova.api.openstack.placement.requestlog [req-f144ce6c-0e5c-40e1-8dfa-9665a22ce61e 78061f04af2741309c970a4c360d1a27 2e6cdfb7870c43e8b5ed0d216430c0f2 - default default] 172.17.45.13 "GET /allocations/66fbf2d6-9081-4124-a0ab-353373ef65c5" status: 200 len: 140 microversion: 1.0 2017-09-27 07:54:29.379 19422 INFO nova.api.openstack.placement.requestlog [req-8fa4cb3e-0aa2-4d8c-ba60-9e53399930d2 78061f04af2741309c970a4c360d1a27 2e6cdfb7870c43e8b5ed0d216430c0f2 - default default] 172.17.45.13 "GET /allocations/1392ea8e-c15d-4429-a8f6-028486c62069" status: 200 len: 139 microversion: 1.0 2017-09-27 07:54:29.461 19424 INFO nova.api.openstack.placement.requestlog [req-91d23b20-2266-4134-8ee8-7c92f0b95597 78061f04af2741309c970a4c360d1a27 2e6cdfb7870c43e8b5ed0d216430c0f2 - default default] 172.17.45.13 "GET /allocations/d07be933-717b-4c6a-bb87-04eff357401f" status: 200 len: 139 microversion: 1.0 2017-09-27 07:54:29.554 19421 INFO nova.api.openstack.placement.requestlog [req-abade4bf-ca08-4057-b5ab-d87f131d332d 78061f04af2741309c970a4c360d1a27 2e6cdfb7870c43e8b5ed0d216430c0f2 - default default] 172.17.45.13 "GET /allocations/be4a8fdb-e752-426c-b289-e3d348c17c32" status: 200 len: 138 microversion: 1.0 2017-09-27 07:54:29.657 19420 INFO nova.api.openstack.placement.requestlog [req-323f54dc-ac28-4132-8d0d-bcec02adbb46 78061f04af2741309c970a4c360d1a27 2e6cdfb7870c43e8b5ed0d216430c0f2 - default default] 172.17.45.13 "GET /allocations/059795b7-1fb1-471d-8c1d-155796829654" status: 200 len: 140 microversion: 1.0 2017-09-27 07:54:29.788 19424 INFO nova.api.openstack.placement.requestlog [req-49a4f749-b4c3-4bc1-8ff8-cda84a76c5d6 78061f04af2741309c970a4c360d1a27 2e6cdfb7870c43e8b5ed0d216430c0f2 - default default] 172.17.45.13 "GET /allocations/2bda36f7-554b-401d-a164-d54c4dbf8541" status: 200 len: 138 microversion: 1.0 2017-09-27 07:54:29.873 19423 INFO nova.api.openstack.placement.requestlog [req-fb403cec-b321-4359-8080-60aac7b45a89 78061f04af2741309c970a4c360d1a27 2e6cdfb7870c43e8b5ed0d216430c0f2 - default default] 172.17.45.13 "GET /allocations/d6dd2de7-be41-45b3-baba-5040a124ce62" status: 200 len: 138 microversion: 1.0 2017-09-27 07:54:29.979 19420 INFO nova.api.openstack.placement.requestlog [req-63204536-50d7-4d22-bbca-fa7c487d50cb 78061f04af2741309c970a4c360d1a27 2e6cdfb7870c43e8b5ed0d216430c0f2 - default default] 172.17.45.13 "GET /allocations/d28b8bcc-c615-47c2-b1a8-bf2442822c6c" status: 200 len: 138 microversion: 1.0 ==> ./nova-scheduler.log <== 2017-09-27 07:53:42.368 21466 INFO nova.scheduler.host_manager [req-2d01b887-5204-420f-88ea-418c64709a69 - - - - -] Successfully synced instances from host \'cmp010\'. 2017-09-27 07:53:44.275 21466 INFO nova.scheduler.host_manager [req-76046843-1bc5-4beb-9880-6757eb786d42 - - - - -] Successfully synced instances from host \'cmp020\'. 2017-09-27 07:54:00.299 21466 INFO nova.scheduler.host_manager [req-deb8cc20-9764-401f-8247-8afb67dfd203 - - - - -] Successfully synced instances from host \'cmp017\'. 2017-09-27 07:54:07.404 21466 INFO nova.scheduler.host_manager [req-80bf05f3-6d17-4e23-9dda-b26c564f54c6 - - - - -] Successfully synced instances from host \'cmp022\'. 2017-09-27 07:54:12.376 21466 INFO nova.scheduler.host_manager [req-13ba5740-05c4-4afc-b79e-f6400ce0fa49 - - - - -] Successfully synced instances from host \'cmp016\'. 2017-09-27 07:54:15.779 21466 INFO nova.scheduler.host_manager [req-20de93ad-3f06-47a0-966e-0edc038f67b7 - - - - -] Successfully synced instances from host \'cmp011\'. 2017-09-27 07:54:22.353 21466 INFO nova.scheduler.host_manager [req-c6526914-d9ee-460c-b837-69779acc93b8 - - - - -] Successfully synced instances from host \'cmp005\'. 2017-09-27 07:54:25.333 21466 INFO nova.scheduler.host_manager [req-b1cfabef-1f65-44a9-bfce-dd31d3aac46b - - - - -] Successfully synced instances from host \'cmp004\'. 2017-09-27 07:54:27.122 21466 INFO nova.scheduler.host_manager [req-aff27712-931a-4293-8ea2-cb17557c96d5 - - - - -] Successfully synced instances from host \'cmp003\'. 2017-09-27 07:54:29.753 21466 INFO nova.scheduler.host_manager [req-f61a4b58-7f3a-4c82-a214-c3ba8e722de5 - - - - -] Successfully synced instances from host \'cmp031\'. '; 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