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

# coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility import re regex = r"^([^\s]*)\s*([^\s]*)\s*([^\s]*)\s*([^\s]*)\s*([^\s]*)\s*([^\s]*)\s*([^\s]*)\s*(.*)$" test_str = ("UID PID PPID C STIME TTY TIME CMD\n" "root 1 0 0 17:19 ? 00:00:00 /sbin/init\n" "root 2 0 0 17:19 ? 00:00:00 [kthreadd]\n" "root 3 2 0 17:19 ? 00:00:00 [migration/0]\n" "root 4 2 0 17:19 ? 00:00:00 [ksoftirqd/0]\n" "root 5 2 0 17:19 ? 00:00:00 [migration/0]\n" "root 6 2 0 17:19 ? 00:00:00 [watchdog/0]\n" "root 7 2 0 17:19 ? 00:00:00 [migration/1]\n" "root 8 2 0 17:19 ? 00:00:00 [migration/1]\n" "root 9 2 0 17:19 ? 00:00:00 [ksoftirqd/1]\n" "root 10 2 0 17:19 ? 00:00:00 [watchdog/1]\n" "root 11 2 0 17:19 ? 00:00:00 [events/0]\n" "root 12 2 0 17:19 ? 00:00:00 [events/1]\n" "root 13 2 0 17:19 ? 00:00:00 [cgroup]\n" "root 14 2 0 17:19 ? 00:00:00 [khelper]\n" "root 15 2 0 17:19 ? 00:00:00 [netns]\n" "root 16 2 0 17:19 ? 00:00:00 [async/mgr]\n" "root 17 2 0 17:19 ? 00:00:00 [pm]\n" "root 18 2 0 17:19 ? 00:00:00 [sync_supers]\n" "root 19 2 0 17:19 ? 00:00:00 [bdi-default]\n" "root 20 2 0 17:19 ? 00:00:00 [kintegrityd/0]\n" "root 21 2 0 17:19 ? 00:00:00 [kintegrityd/1]\n" "root 22 2 0 17:19 ? 00:00:00 [kblockd/0]\n" "root 23 2 0 17:19 ? 00:00:00 [kblockd/1]\n" "root 24 2 0 17:19 ? 00:00:00 [kacpid]\n" "root 25 2 0 17:19 ? 00:00:00 [kacpi_notify]\n" "root 26 2 0 17:19 ? 00:00:00 [kacpi_hotplug]\n" "root 27 2 0 17:19 ? 00:00:00 [ata_aux]\n" "root 28 2 0 17:19 ? 00:00:00 [ata_sff/0]\n" "root 29 2 0 17:19 ? 00:00:00 [ata_sff/1]\n" "root 30 2 0 17:19 ? 00:00:00 [ksuspend_usbd]\n" "root 31 2 0 17:19 ? 00:00:00 [khubd]\n" "root 32 2 0 17:19 ? 00:00:00 [kseriod]\n" "root 33 2 0 17:19 ? 00:00:00 [md/0]\n" "root 34 2 0 17:19 ? 00:00:00 [md/1]\n" "root 35 2 0 17:19 ? 00:00:00 [md_misc/0]\n" "root 36 2 0 17:19 ? 00:00:00 [md_misc/1]\n" "root 37 2 0 17:19 ? 00:00:00 [linkwatch]\n" "root 38 2 0 17:19 ? 00:00:00 [khungtaskd]\n" "root 39 2 0 17:19 ? 00:00:00 [kswapd0]\n" "root 40 2 0 17:19 ? 00:00:00 [ksmd]\n" "root 41 2 0 17:19 ? 00:00:00 [khugepaged]\n" "root 42 2 0 17:19 ? 00:00:00 [aio/0]\n" "root 43 2 0 17:19 ? 00:00:00 [aio/1]\n" "root 44 2 0 17:19 ? 00:00:00 [crypto/0]\n" "root 45 2 0 17:19 ? 00:00:00 [crypto/1]\n" "root 50 2 0 17:19 ? 00:00:00 [kthrotld/0]\n" "root 51 2 0 17:19 ? 00:00:00 [kthrotld/1]\n" "root 53 2 0 17:19 ? 00:00:00 [kpsmoused]\n" "root 54 2 0 17:19 ? 00:00:00 [usbhid_resumer]\n" "root 85 2 0 17:19 ? 00:00:00 [kstriped]\n" "root 153 2 0 17:19 ? 00:00:00 [scsi_eh_0]\n" "root 154 2 0 17:19 ? 00:00:00 [scsi_eh_1]\n" "root 171 2 0 17:19 ? 00:00:00 [scsi_eh_2]\n" "root 250 2 0 17:19 ? 00:00:00 [kdmflush]\n" "root 251 2 0 17:19 ? 00:00:00 [kdmflush]\n" "root 269 2 0 17:19 ? 00:00:00 [jbd2/dm-0-8]\n" "root 270 2 0 17:19 ? 00:00:00 [ext4-dio-unwrit]\n" "root 366 1 0 17:19 ? 00:00:00 /sbin/udevd -d\n" "root 898 2 0 17:19 ? 00:00:00 [jbd2/sda1-8]\n" "root 899 2 0 17:19 ? 00:00:00 [ext4-dio-unwrit]\n" "root 910 2 0 17:19 ? 00:00:00 [flush-253:0]\n" "root 951 2 0 17:19 ? 00:00:00 [kauditd]\n" "root 1017 1 0 17:19 ? 00:00:00 auditd\n" "root 1042 1 0 17:19 ? 00:00:00 /sbin/rsyslogd -i /var/run/syslogd.pid -c 5\n" "root 1071 1 0 17:19 ? 00:00:00 irqbalance --pid=/var/run/irqbalance.pid\n" "rpc 1091 1 0 17:19 ? 00:00:00 rpcbind\n" "root 1205 1 0 17:19 ? 00:00:00 lldpad -d\n" "root 1214 2 0 17:19 ? 00:00:00 [scsi_tgtd/0]\n" "root 1215 2 0 17:19 ? 00:00:00 [scsi_tgtd/1]\n" "root 1222 2 0 17:19 ? 00:00:00 [fc_exch_workque]\n" "root 1223 2 0 17:19 ? 00:00:00 [fc_rport_eq]\n" "root 1226 2 0 17:19 ? 00:00:00 [fcoe_work/0]\n" "root 1227 2 0 17:19 ? 00:00:00 [fcoe_work/1]\n" "root 1228 2 0 17:19 ? 00:00:00 [fcoethread/0]\n" "root 1229 2 0 17:19 ? 00:00:00 [fcoethread/1]\n" "root 1230 2 0 17:19 ? 00:00:00 [cnic_wq]\n" "root 1231 2 0 17:19 ? 00:00:00 [bnx2fc]\n" "root 1232 2 0 17:19 ? 00:00:00 [bnx2fc_l2_threa]\n" "root 1233 2 0 17:19 ? 00:00:00 [bnx2fc_thread/0]\n" "root 1234 2 0 17:19 ? 00:00:00 [bnx2fc_thread/1]\n" "root 1237 1 0 17:19 ? 00:00:00 /usr/sbin/fcoemon --syslog\n" "dbus 1249 1 0 17:19 ? 00:00:00 dbus-daemon --system\n" "root 1260 1 0 17:19 ? 00:00:00 NetworkManager --pid-file=/var/run/NetworkManager/NetworkManager.pid\n" "root 1267 1 0 17:19 ? 00:00:00 /usr/sbin/modem-manager\n" "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\n" "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\n" "rpcuser 1285 1 0 17:19 ? 00:00:00 rpc.statd\n" "root 1319 1 0 17:19 ? 00:00:00 cupsd -C /etc/cups/cupsd.conf\n" "root 1391 1 0 17:19 ? 00:00:00 /usr/sbin/acpid\n" "68 1413 1 0 17:19 ? 00:00:00 hald\n" "root 1414 1413 0 17:19 ? 00:00:00 hald-runner\n" "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\n" "root 1468 1414 0 17:19 ? 00:00:00 hald-addon-storage: polling /dev/sr0 (every 2 sec)\n" "68 1471 1414 0 17:19 ? 00:00:00 hald-addon-acpi: listening on acpid socket /var/run/acpid.socket\n" "root 1493 1 0 17:19 ? 00:00:00 pcscd\n" "root 1509 1 0 17:19 ? 00:00:00 automount --pid-file /var/run/autofs.pid\n" "root 1586 1 0 17:19 ? 00:00:00 /usr/sbin/sshd\n" "ntp 1594 1 0 17:19 ? 00:00:00 ntpd -u ntp:ntp -p /var/run/ntpd.pid -g\n" "root 1698 1 0 17:19 ? 00:00:00 /usr/libexec/postfix/master\n" "postfix 1703 1698 0 17:19 ? 00:00:00 qmgr -l -t fifo -u\n" "root 1722 1 0 17:19 ? 00:00:00 /usr/sbin/abrtd\n" "root 1730 1 0 17:19 ? 00:00:00 abrt-dump-oops -d /var/spool/abrt -rwx /var/log/messages\n" "root 1738 1 0 17:19 ? 00:00:00 crond\n" "root 1749 1 0 17:19 ? 00:00:00 /usr/sbin/atd\n" "root 1776 1 0 17:19 ? 00:00:00 /usr/sbin/certmonger -S -p /var/run/certmonger.pid\n" "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\n" "root 1841 1 0 17:19 ? 00:00:00 login -- root\n" "root 1843 1 0 17:19 tty2 00:00:00 /sbin/mingetty /dev/tty2\n" "root 1847 1 0 17:19 tty3 00:00:00 /sbin/mingetty /dev/tty3\n" "root 1849 1 0 17:19 tty4 00:00:00 /sbin/mingetty /dev/tty4\n" "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_\n" "root 1855 1 0 17:19 tty5 00:00:00 /sbin/mingetty /dev/tty5\n" "root 1857 1 0 17:19 tty6 00:00:00 /sbin/mingetty /dev/tty6\n" "root 1865 366 0 17:19 ? 00:00:00 /sbin/udevd -d\n" "root 1866 366 0 17:19 ? 00:00:00 /sbin/udevd -d\n" "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\n" "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_\n" "root 2039 1 0 17:20 ? 00:00:00 /usr/sbin/console-kit-daemon --no-daemon\n" "root 2106 1841 0 17:20 tty1 00:00:00 -bash\n" "root 2132 1586 0 17:20 ? 00:00:00 sshd: strs [priv]\n" "strs 2136 2132 0 17:21 ? 00:00:00 sshd: strs@pts/0\n" "strs 2137 2136 0 17:21 pts/0 00:00:00 -bash\n" "postfix 2171 1698 0 17:23 ? 00:00:00 pickup -l -t fifo -u\n" "strs 2189 2137 0 17:27 pts/0 00:00:00 ps -ef\n") matches = re.finditer(regex, test_str, re.MULTILINE | re.IGNORECASE) 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