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
No Match

r"
"
g

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" test_str = ("libmono-system-xml4.0-cil/now 3.2.8+dfsg-4ubuntu1.1 all [installed,local]\n\n" "usbmuxd/kali-rolling,now 1.1.0-2+b1 amd64 [installed,automatic]\n\n\n" "usbutils/kali-rolling,now 1:007-4 amd64 [installed]\n" "util-linux/kali-rolling,now 2.28-6 amd64 [installed]\n" "uuid-runtime/kali-rolling,now 2.28-6 amd64 [installed,automatic]\n" "va-driver-all/kali-rolling,now 1.7.1-2 amd64 [installed,automatic]\n" "vboot-kernel-utils/kali-rolling,now 0~R52-8350.B-1 amd64 [installed,automatic]\n" "vboot-utils/kali-rolling,now 0~R52-8350.B-1 amd64 [installed,automatic]\n" "vdpau-driver-all/kali-rolling,now 1.1.1-3 amd64 [installed,automatic]\n" "libhttpclient-java/now 4.3.3-1 all [installed,local]\n" "libhttpcore-java/now 4.3.2-2 all [installed,local]\n" "libhwloc-dev/now 1.8-1ubuntu1.14.04.1 amd64 [installed,local]\n" "libhwloc-plugins/now 1.8-1ubuntu1.14.04.1 amd64 [installed,local]\n" "libhwloc5/now 1.8-1ubuntu1.14.04.1 amd64 [installed,local]\n" "libhx509-5-heimdal/now 1.6~git20131207+dfsg-1ubuntu1.1 amd64 [installed,local]\n" "libibverbs-dev/now 1.1.7-1ubuntu1.1 amd64 [installed,local]\n" "libibverbs1/now 1.1.7-1ubuntu1.1 amd64 [installed,local]\n" "libice-dev/now 2:1.0.8-2 amd64 [installed,local]\n" "libice6/now 2:1.0.8-2 amd64 [installed,local]\n" "libicu-dev/now 52.1-3ubuntu0.4 amd64 [installed,local]\n" "libicu52/now 52.1-3ubuntu0.4 amd64 [installed,local]\n" "libid3tag0/now 0.15.1b-10ubuntu1 amd64 [installed,local]\n" "libidl-common/now 0.8.14-0.2ubuntu4 all [installed,local]\n" "libidl0/now 0.8.14-0.2ubuntu4 amd64 [installed,local]\n" "libidn11/now 1.28-1ubuntu2 amd64 [installed,local]\n" "libieee1284-3/now 0.2.11-12 amd64 [installed,local]\n" "libmono-system-windows4.0-cil/now 3.2.8+dfsg-4ubuntu1.1 all [installed,local]\n" "libmono-system-xaml4.0-cil/now 3.2.8+dfsg-4ubuntu1.1 all [installed,local]\n" "libmono-system-xml-linq4.0-cil/now 3.2.8+dfsg-4ubuntu1.1 all [installed,local]\n" "libmono-system-xml-serialization4.0-cil/now 3.2.8+dfsg-4ubuntu1.1 all [installed,local]\n\n" "libmono-system2.0-cil/now 3.2.8+dfsg-4ubuntu1.1 all [installed,local]\n" "libmono-system4.0-cil/now 3.2.8+dfsg-4ubuntu1.1 all [installed,local]\n" "libmono-tasklets2.0-cil/now 3.2.8+dfsg-4ubuntu1.1 all [installed,local]\n" "libmono-tasklets4.0-cil/now 3.2.8+dfsg-4ubuntu1.1 all [installed,local]\n" "libmono-wcf3.0a-cil/now 3.2.8+dfsg-4ubuntu1.1 all [installed,local]\n" "libmono-web4.0-cil/now 3.2.8+dfsg-4ubuntu1.1 all [installed,local]\n" "libmono-webbrowser2.0-cil/now 3.2.8+dfsg-4ubuntu1.1 all [installed,local]\n" "libmono-webbrowser4.0-cil/now 3.2.8+dfsg-4ubuntu1.1 all [installed,local]\n" "libmono-webmatrix-data4.0-cil/now 3.2.8+dfsg-4ubuntu1.1 all [installed,local]\n" "libmono-windowsbase3.0-cil/now 3.2.8+dfsg-4ubuntu1.1 all [installed,local]\n" "nginx/now 1.4.6-1ubuntu3.3 all [installed,local]\n" "nginx-common/now 1.4.6-1ubuntu3.3 all [installed,local]\n" "nginx-core/now 1.4.6-1ubuntu3.3 amd64 [installed,local]\n" "novnc/now 1:0.4+dfsg+1+20131010+gitf68af8af3d-2 all [installed,local]\n" "ntfs-3g/now 1:2013.1.13AR.1-2ubuntu2 amd64 [installed,local]\n" "ntpdate/now 1:4.2.6.p5+dfsg-3ubuntu2.14.04.6 amd64 [installed,local]\n" "ocl-icd-libopencl1/now 2.1.3-4 amd64 [installed,local]\n" "openjdk-7-doc/now 7u91-2.6.3-0ubuntu0.14.04.1 all [installed,local]\n" "openjdk-7-jdk/now 7u91-2.6.3-0ubuntu0.14.04.1 amd64 [installed,local]\n" "openjdk-7-jre/now 7u91-2.6.3-0ubuntu0.14.04.1 amd64 [installed,local]\n" "openjdk-7-jre-headless/now 7u91-2.6.3-0ubuntu0.14.04.1 amd64 [installed,local]\n" "openmpi-bin/now 1.6.5-8 amd64 [installed,local]\n" "openmpi-common/now 1.6.5-8 all [installed,local]\n" "openssh-client/now 1:6.6p1-2ubuntu2.3 amd64 [installed,local]\n" "openssl/now 1.0.1f-1ubuntu2.16 amd64 [installed,local]\n" "parallel/now 20130922-1 all [installed,local]\n" "parted/now 2.3-19ubuntu1.14.04.1 amd64 [installed,local]\n" "passwd/now 1:4.1.5.1-1ubuntu9.1 amd64 [installed,local]\n" "patch/now 2.7.1-4ubuntu2.3 amd64 [installed,local]\n" "perl/now 5.18.2-2ubuntu1 amd64 [installed,local]\n" "perl-base/now 5.18.2-2ubuntu1 amd64 [installed,local]\n" "perl-modules/now 5.18.2-2ubuntu1 all [installed,local]\n" "pgf/now 2.10-1 all [installed,local]\n" "php-gettext/now 1.0.11-1 all [installed,local]\n" "php-pear/now 5.5.9+dfsg-1ubuntu4.14 all [installed,local]\n" "php5/now 5.5.9+dfsg-1ubuntu4.14 all [installed,local]\n" "php5-cli/now 5.5.9+dfsg-1ubuntu4.14 amd64 [installed,local]\n" "php5-common/now 5.5.9+dfsg-1ubuntu4.14 amd64 [installed,local]\n" "php5-curl/now 5.5.9+dfsg-1ubuntu4.14 amd64 [installed,local]\n" "php5-dev/now 5.5.9+dfsg-1ubuntu4.14 amd64 [installed,local]\n" "php5-fpm/now 5.5.9+dfsg-1ubuntu4.14 amd64 [installed,local]\n" "php5-gd/now 5.5.9+dfsg-1ubuntu4.14 amd64 [installed,local]\n" "php5-intl/now 5.5.9+dfsg-1ubuntu4.14 amd64 [installed,local]\n" "php5-json/now 1.3.2-2build1 amd64 [installed,local]\n" "php5-mcrypt/now 5.4.6-0ubuntu5 amd64 [installed,local]\n" "php5-mysqlnd/now 5.5.9+dfsg-1ubuntu4.14 amd64 [installed,local]\n" "php5-pgsql/now 5.5.9+dfsg-1ubuntu4.14 amd64 [installed,local]\n" "php5-readline/now 5.5.9+dfsg-1ubuntu4.14 amd64 [installed,local]\n" "php5-sqlite/now 5.5.9+dfsg-1ubuntu4.14 amd64 [installed,local]\n" "php5-tidy/now 5.5.9+dfsg-1ubuntu4.14 amd64 [installed,local]\n" "php5-xmlrpc/now 5.5.9+dfsg-1ubuntu4.14 amd64 [installed,local]\n" "php5-xsl/now 5.5.9+dfsg-1ubuntu4.14 amd64 [installed,local]\n" "phpmyadmin/now 4:4.0.10-1 all [installed,local]\n" "phppgadmin/now 5.1-1 all [installed,local]\n" "pkg-config/now 0.26-1ubuntu4 amd64 [installed,local]\n" "pkg-php-tools/now 1.11 all [installed,local]\n" "plymouth/now 0.8.8-0ubuntu17.1 amd64 [installed,local]\n" "po-debconf/now 1.0.16+nmu2ubuntu1 all [installed,local]\n" "policykit-1/now 0.105-4ubuntu3.14.04.1 amd64 [installed,local]\n" "policykit-1-gnome/now 0.105-1ubuntu4 amd64 [installed,local]\n" "poppler-data/now 0.4.6-4 all [installed,local]\n" "postgresql-9.3/now 9.3.10-0ubuntu0.14.04 amd64 [installed,local]\n" "postgresql-client-9.3/now 9.3.10-0ubuntu0.14.04 amd64 [installed,local]\n" "postgresql-client-common/now 154ubuntu1 all [installed,local]\n" "postgresql-common/now 154ubuntu1 all [installed,local]\n" "postgresql-doc/now 9.3+154ubuntu1 all [installed,local]\n" "postgresql-doc-9.3/now 9.3.10-0ubuntu0.14.04 all [installed,local]\n" "procps/now 1:3.3.9-1ubuntu2.2 amd64 [installed,local]\n" "prosper/now 1.00.4+cvs.2007.05.01-4 all [installed,local]\n" "ps2eps/now 1.68-1build1 amd64 [installed,local]\n" "psmisc/now 22.20-1ubuntu2 amd64 [installed,local]\n" "python/now 2.7.5-5ubuntu3 amd64 [installed,local]\n" "python-amqp/now 1.3.3-1ubuntu1.1 all [installed,local]\n" "python-amqplib/now 1.0.2-1 all [installed,local]\n" "python-anyjson/now 0.3.3-1build1 all [installed,local]\n" "python-apt/now 0.9.3.5ubuntu2 amd64 [installed,local]\n" "python-apt-common/now 0.9.3.5ubuntu2 all [installed,local]\n" "python-babel/now 1.3+dfsg.1-2ubuntu2 all [installed,local]\n" "python-babel-localedata/now 1.3+dfsg.1-2ubuntu2 all [installed,local]\n" "python-boto/now 2.20.1-2ubuntu2 all [installed,local]\n" "python-bzrlib/now 2.6.0+bzr6593-1ubuntu1.1 amd64 [installed,local]\n" "python-cairo/now 1.8.8-1ubuntu5 amd64 [installed,local]\n" "python-chardet/now 2.0.1-2build2 all [installed,local]\n" "python-chardet-whl/now 2.2.1-2~ubuntu1 all [installed,local]\n" "python-cinderclient/now 1:1.0.8-0ubuntu2 all [installed,local]\n" "python-cliff/now 1.4.5-1ubuntu2 all [installed,local]\n" "python-cliff-doc/now 1.4.5-1ubuntu2 all [installed,local]\n" "python-cmd2/now 0.6.7-2fakesync1build1 all [installed,local]\n" "python-colorama/now 0.2.5-0.1ubuntu2 all [installed,local]\n" "python-colorama-whl/now 0.2.5-0.1ubuntu2 all [installed,local]\n" "python-configobj/now 4.7.2+ds-5build1 all [installed,local]\n" "python-crypto/now 2.6.1-4build1 amd64 [installed,local]\n" "python-dateutil/now 1.5+dfsg-1ubuntu1 all [installed,local]\n" "python-dbus/now 1.2.0-2build2 amd64 [installed,local]\n" "python-dbus-dev/now 1.2.0-2build2 all [installed,local]\n" "python-decorator/now 3.4.0-2build1 all [installed,local]\n" "python-dev/now 2.7.5-5ubuntu3 amd64 [installed,local]\n" "python-distlib/now 0.1.8-1ubuntu1 all [installed,local]\n" "python-distlib-whl/now 0.1.8-1ubuntu1 all [installed,local]\n" "python-dns/now 2.3.6-3 all [installed,local]\n" "python-eventlet/now 0.13.0-1ubuntu2.3 all [installed,local]\n" "x11proto-xext-dev/now 7.3.0-1 all [installed,local]\n" "x11vnc/now 0.9.13-1.1 amd64 [installed,local]\n" "x11vnc-data/now 0.9.13-1.1 all [installed,local]\n" "xauth/now 1:1.0.7-1ubuntu1 amd64 [installed,local]\n" "xbitmaps/now 1.1.1-2 all [installed,local]\n" "xdg-utils/now 1.1.0~rc1-2ubuntu7.1 all [installed,local]\n" "xfonts-base/now 1:1.0.3 all [installed,local]\n" "xfonts-encodings/now 1:1.0.4-1ubuntu1 all [installed,local]\n" "xfonts-terminus/now 4.38-1fakesync1 all [installed,local]\n" "xfonts-utils/now 1:7.7+1 amd64 [installed,local]\n" "xkb-data/now 2.10.1-1ubuntu1 all [installed,local]\n" "xml-core/now 0.13+nmu2 all [installed,local]\n" "xorg-sgml-doctools/now 1:1.11-1 all [installed,local]\n" "xserver-common/now 2:1.15.1-0ubuntu2.7 all [installed,local]\n" "xsltproc/now 1.1.28-2build1 amd64 [installed,local]\n" "xterm/now 297-1ubuntu1 amd64 [installed,local]\n" "xtrans-dev/now 1.3.5-1~ubuntu14.04.1 all [installed,local]\n" "xul-ext-ubufox/now 3.2-0ubuntu0.14.04.1 all [installed,local]\n" "xvfb/now 2:1.15.1-0ubuntu2.7 amd64 [installed,local]\n" "xz-utils/now 5.1.1alpha+20120614-2ubuntu2 amd64 [installed,local]\n" "zip/now 3.0-8 amd64 [installed,local]\n" "zlib1g/now 1:1.2.8.dfsg-1ubuntu1 amd64 [installed,local]\n" "zlib1g-dev/now 1:1.2.8.dfsg-1ubuntu1 amd64 [installed,local]\n") matches = re.finditer(regex, test_str) 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