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

/
/
gmi

Test String

Code Generator

Generated Code

$re = '/^([^\s]*)\s*([^\s]*)\s*([^\s]*)\s*([^\s]*)\s*([^\s]*)\s*([^\s]*)\s*([^\s]*)\s*(.*)$/mi'; $str = 'UID PID PPID C STIME TTY TIME CMD root 1 0 0 17:19 ? 00:00:00 /sbin/init root 2 0 0 17:19 ? 00:00:00 [kthreadd] root 3 2 0 17:19 ? 00:00:00 [migration/0] root 4 2 0 17:19 ? 00:00:00 [ksoftirqd/0] root 5 2 0 17:19 ? 00:00:00 [migration/0] root 6 2 0 17:19 ? 00:00:00 [watchdog/0] root 7 2 0 17:19 ? 00:00:00 [migration/1] root 8 2 0 17:19 ? 00:00:00 [migration/1] root 9 2 0 17:19 ? 00:00:00 [ksoftirqd/1] root 10 2 0 17:19 ? 00:00:00 [watchdog/1] root 11 2 0 17:19 ? 00:00:00 [events/0] root 12 2 0 17:19 ? 00:00:00 [events/1] root 13 2 0 17:19 ? 00:00:00 [cgroup] root 14 2 0 17:19 ? 00:00:00 [khelper] root 15 2 0 17:19 ? 00:00:00 [netns] root 16 2 0 17:19 ? 00:00:00 [async/mgr] root 17 2 0 17:19 ? 00:00:00 [pm] root 18 2 0 17:19 ? 00:00:00 [sync_supers] root 19 2 0 17:19 ? 00:00:00 [bdi-default] root 20 2 0 17:19 ? 00:00:00 [kintegrityd/0] root 21 2 0 17:19 ? 00:00:00 [kintegrityd/1] root 22 2 0 17:19 ? 00:00:00 [kblockd/0] root 23 2 0 17:19 ? 00:00:00 [kblockd/1] root 24 2 0 17:19 ? 00:00:00 [kacpid] root 25 2 0 17:19 ? 00:00:00 [kacpi_notify] root 26 2 0 17:19 ? 00:00:00 [kacpi_hotplug] root 27 2 0 17:19 ? 00:00:00 [ata_aux] root 28 2 0 17:19 ? 00:00:00 [ata_sff/0] root 29 2 0 17:19 ? 00:00:00 [ata_sff/1] root 30 2 0 17:19 ? 00:00:00 [ksuspend_usbd] root 31 2 0 17:19 ? 00:00:00 [khubd] root 32 2 0 17:19 ? 00:00:00 [kseriod] root 33 2 0 17:19 ? 00:00:00 [md/0] root 34 2 0 17:19 ? 00:00:00 [md/1] root 35 2 0 17:19 ? 00:00:00 [md_misc/0] root 36 2 0 17:19 ? 00:00:00 [md_misc/1] root 37 2 0 17:19 ? 00:00:00 [linkwatch] root 38 2 0 17:19 ? 00:00:00 [khungtaskd] root 39 2 0 17:19 ? 00:00:00 [kswapd0] root 40 2 0 17:19 ? 00:00:00 [ksmd] root 41 2 0 17:19 ? 00:00:00 [khugepaged] root 42 2 0 17:19 ? 00:00:00 [aio/0] root 43 2 0 17:19 ? 00:00:00 [aio/1] root 44 2 0 17:19 ? 00:00:00 [crypto/0] root 45 2 0 17:19 ? 00:00:00 [crypto/1] root 50 2 0 17:19 ? 00:00:00 [kthrotld/0] root 51 2 0 17:19 ? 00:00:00 [kthrotld/1] root 53 2 0 17:19 ? 00:00:00 [kpsmoused] root 54 2 0 17:19 ? 00:00:00 [usbhid_resumer] root 85 2 0 17:19 ? 00:00:00 [kstriped] root 153 2 0 17:19 ? 00:00:00 [scsi_eh_0] root 154 2 0 17:19 ? 00:00:00 [scsi_eh_1] root 171 2 0 17:19 ? 00:00:00 [scsi_eh_2] root 250 2 0 17:19 ? 00:00:00 [kdmflush] root 251 2 0 17:19 ? 00:00:00 [kdmflush] root 269 2 0 17:19 ? 00:00:00 [jbd2/dm-0-8] root 270 2 0 17:19 ? 00:00:00 [ext4-dio-unwrit] root 366 1 0 17:19 ? 00:00:00 /sbin/udevd -d root 898 2 0 17:19 ? 00:00:00 [jbd2/sda1-8] root 899 2 0 17:19 ? 00:00:00 [ext4-dio-unwrit] root 910 2 0 17:19 ? 00:00:00 [flush-253:0] root 951 2 0 17:19 ? 00:00:00 [kauditd] root 1017 1 0 17:19 ? 00:00:00 auditd root 1042 1 0 17:19 ? 00:00:00 /sbin/rsyslogd -i /var/run/syslogd.pid -c 5 root 1071 1 0 17:19 ? 00:00:00 irqbalance --pid=/var/run/irqbalance.pid rpc 1091 1 0 17:19 ? 00:00:00 rpcbind root 1205 1 0 17:19 ? 00:00:00 lldpad -d root 1214 2 0 17:19 ? 00:00:00 [scsi_tgtd/0] root 1215 2 0 17:19 ? 00:00:00 [scsi_tgtd/1] root 1222 2 0 17:19 ? 00:00:00 [fc_exch_workque] root 1223 2 0 17:19 ? 00:00:00 [fc_rport_eq] root 1226 2 0 17:19 ? 00:00:00 [fcoe_work/0] root 1227 2 0 17:19 ? 00:00:00 [fcoe_work/1] root 1228 2 0 17:19 ? 00:00:00 [fcoethread/0] root 1229 2 0 17:19 ? 00:00:00 [fcoethread/1] root 1230 2 0 17:19 ? 00:00:00 [cnic_wq] root 1231 2 0 17:19 ? 00:00:00 [bnx2fc] root 1232 2 0 17:19 ? 00:00:00 [bnx2fc_l2_threa] root 1233 2 0 17:19 ? 00:00:00 [bnx2fc_thread/0] root 1234 2 0 17:19 ? 00:00:00 [bnx2fc_thread/1] root 1237 1 0 17:19 ? 00:00:00 /usr/sbin/fcoemon --syslog dbus 1249 1 0 17:19 ? 00:00:00 dbus-daemon --system root 1260 1 0 17:19 ? 00:00:00 NetworkManager --pid-file=/var/run/NetworkManager/NetworkManager.pid root 1267 1 0 17:19 ? 00:00:00 /usr/sbin/modem-manager root 1270 1260 0 17:19 ? 00:00:00 /sbin/dhclient -d -4 -sf /usr/libexec/nm-dhcp-client.action -pf /var/run/dhclient-eth1.pid -lf /var/lib/dhclient/dhclient-6f1cf570-d907 root 1279 1 0 17:19 ? 00:00:00 /usr/sbin/wpa_supplicant -c /etc/wpa_supplicant/wpa_supplicant.conf -B -u -f /var/log/wpa_supplicant.log -P /var/run/wpa_supplicant.pid rpcuser 1285 1 0 17:19 ? 00:00:00 rpc.statd root 1319 1 0 17:19 ? 00:00:00 cupsd -C /etc/cups/cupsd.conf root 1391 1 0 17:19 ? 00:00:00 /usr/sbin/acpid 68 1413 1 0 17:19 ? 00:00:00 hald root 1414 1413 0 17:19 ? 00:00:00 hald-runner root 1467 1414 0 17:19 ? 00:00:00 hald-addon-input: Listening on /dev/input/event3 /dev/input/event5 /dev/input/event1 /dev/input/event0 root 1468 1414 0 17:19 ? 00:00:00 hald-addon-storage: polling /dev/sr0 (every 2 sec) 68 1471 1414 0 17:19 ? 00:00:00 hald-addon-acpi: listening on acpid socket /var/run/acpid.socket root 1493 1 0 17:19 ? 00:00:00 pcscd root 1509 1 0 17:19 ? 00:00:00 automount --pid-file /var/run/autofs.pid root 1586 1 0 17:19 ? 00:00:00 /usr/sbin/sshd ntp 1594 1 0 17:19 ? 00:00:00 ntpd -u ntp:ntp -p /var/run/ntpd.pid -g root 1698 1 0 17:19 ? 00:00:00 /usr/libexec/postfix/master postfix 1703 1698 0 17:19 ? 00:00:00 qmgr -l -t fifo -u root 1722 1 0 17:19 ? 00:00:00 /usr/sbin/abrtd root 1730 1 0 17:19 ? 00:00:00 abrt-dump-oops -d /var/spool/abrt -rwx /var/log/messages root 1738 1 0 17:19 ? 00:00:00 crond root 1749 1 0 17:19 ? 00:00:00 /usr/sbin/atd root 1776 1 0 17:19 ? 00:00:00 /usr/sbin/certmonger -S -p /var/run/certmonger.pid strs 1823 1 0 17:19 ? 00:00:00 /opt/strs/bin_strs550/platform/ManagementGateway -managementroot /opt/strs/root_550_1821 -managementport 28000 -env file://.environment root 1841 1 0 17:19 ? 00:00:00 login -- root root 1843 1 0 17:19 tty2 00:00:00 /sbin/mingetty /dev/tty2 root 1847 1 0 17:19 tty3 00:00:00 /sbin/mingetty /dev/tty3 root 1849 1 0 17:19 tty4 00:00:00 /sbin/mingetty /dev/tty4 strs 1852 1 0 17:19 ? 00:00:00 /opt/strs/bin_strs561/platform/ManagementGateway -managementroot /opt/strs/root_561_228 -managementport 28200 -env file://.environment_ root 1855 1 0 17:19 tty5 00:00:00 /sbin/mingetty /dev/tty5 root 1857 1 0 17:19 tty6 00:00:00 /sbin/mingetty /dev/tty6 root 1865 366 0 17:19 ? 00:00:00 /sbin/udevd -d root 1866 366 0 17:19 ? 00:00:00 /sbin/udevd -d strs 1922 1 0 17:19 ? 00:00:00 /opt/strs/bin_strs550/platform/ManagementNanny -managementroot /opt/strs/root_550_1821 -managementport 28000 -env file://.environment_5 strs 2023 1 0 17:20 ? 00:00:00 /opt/strs/bin_strs561/platform/ManagementNanny -managementroot /opt/strs/root_561_228 -managementport 28200 -env file://.environment_5_ root 2039 1 0 17:20 ? 00:00:00 /usr/sbin/console-kit-daemon --no-daemon root 2106 1841 0 17:20 tty1 00:00:00 -bash root 2132 1586 0 17:20 ? 00:00:00 sshd: strs [priv] strs 2136 2132 0 17:21 ? 00:00:00 sshd: strs@pts/0 strs 2137 2136 0 17:21 pts/0 00:00:00 -bash postfix 2171 1698 0 17:23 ? 00:00:00 pickup -l -t fifo -u strs 2189 2137 0 17:27 pts/0 00:00:00 ps -ef '; 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