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

/
/
gsi

Test String

Substitution

Processing...

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".*?Name: (?<name>.*?)\nversion: (?<num>.*?)\n.*?created on: (?<date>.*?)\n.*?" test_str = ("Loading repository data...\n" "Reading installed packages...\n\n\n" "Information for patch sdksp3-rpm-201406:\n\n" "Name: sdksp3-rpm-201406\n" "Version: 9435\n" "Arch: noarch\n" "Vendor: maint-coord@suse.de\n" "Status: Needed\n" "Category: recommended\n" "Created On: Thu Jun 26 11:27:51 2014\n" "Reboot Required: No\n" "Package Manager Restart Required: Yes\n" "Interactive: No\n" "Summary: YOU update for popt\n" "Description:\n\n" "This update for RPM provides the following fixes and enhancements:\n\n" " * Add query support for the new weak dependency tags used in SLE 12.\n" " (bnc#884373)\n" " * Remove harmful --target option passing from the configure macro.\n" " (bnc#870358)\n" " * Disable broken ldconfig skipping code. (bnc#725478)\n\n" "Additionally, one issue was fixed in the rpm-python sub-package:\n\n" " * Return a PyLong for installsize and archivesize if a PyInt would be\n" " negative. This could make createrepo(8) generate repository metadata\n" " with negative sizes for some packages. (bnc#882489)\n\n\n" "Provides:\n" "patch:sdksp3-rpm-201406 == 9435\n\n" "Conflicts:\n" "popt-devel.x86_64 < 1.7-37.58.1\n" "rpm-32bit.x86_64 < 4.4.2.3-37.58.1\n" "rpm-devel.x86_64 < 4.4.2.3-37.58.1\n" "Loading repository data...\n" "Reading installed packages...\n\n\n" "Information for patch sdksp3-rpm-201501:\n\n" "Name: sdksp3-rpm-201501\n" "Version: 10143\n" "Arch: noarch\n" "Vendor: maint-coord@suse.de\n" "Status: Needed\n" "Category: recommended\n" "Created On: Fri Jan 9 08:15:33 2015\n" "Reboot Required: No\n" "Package Manager Restart Required: Yes\n" "Interactive: No\n" "Summary: Recommended update for rpm\n" "Description:\n\n" "This update for \"rpm\" fixes installation of packages when there are spaces\n" "in the path or file name and the option \"--noglob\" is used.\n\n\n" "Provides:\n" "patch:sdksp3-rpm-201501 == 10143\n\n" "Conflicts:\n" "popt-devel.x86_64 < 1.7-37.62.9\n" "rpm-32bit.x86_64 < 4.4.2.3-37.62.9\n" "rpm-devel.x86_64 < 4.4.2.3-37.62.9\n" "Loading repository data...\n" "Reading installed packages...\n\n\n" "Information for patch sdksp3-softwaremgmt-201503:\n\n" "Name: sdksp3-softwaremgmt-201503\n" "Version: 10576\n" "Arch: noarch\n" "Vendor: maint-coord@suse.de\n" "Status: Needed\n" "Category: recommended\n" "Created On: Tue Apr 7 09:29:23 2015\n" "Reboot Required: No\n" "Package Manager Restart Required: Yes\n" "Interactive: No\n" "Summary: YOU update for the Software Update Stack\n" "Description:\n\n" "This collective update for the Software Update Stack provides several fixes\n" "and enhancements.\n\n" "libsatsolver:\n\n" " * Support product end-of-life attribute. (FATE#300591)\n\n" "libzypp:\n\n" " * Improve conflict message for locked packages. (bsc#828631)\n" " * Add Product::endOfLife attribute. (FATE#316172, FATE#300591)\n\n" "PackageKit:\n\n" " * When a proxy server is configured, a default route is not mandatory.\n" " (bsc#910462)\n\n" "rpm:\n\n" " * Allow noscripts and notriggers on verify. (bsc#803669)\n\n" "yast2-pkg-bindings:\n\n" " * Use alias from URL query parameter if present. (bsc#892431)\n\n" "zypper:\n\n" " * A date limit must ignore newer patch candidates. (bsc#919709)\n" " * Show locked packages in zypper summary. (FATE#318256)\n" " * Refresh plugin services on 'lr' 'ls -r' and 'ref'. (bsc#893294,\n" " FATE#317863)\n" " * Enhance 'Digest verification failed' dialog. (FATE#315008)\n" " * Add Product::endOfLife attribute. (FATE#316172, FATE#300591)\n\n\n" "Provides:\n" "patch:sdksp3-softwaremgmt-201503 == 10576\n\n" "Conflicts:\n" "PackageKit-devel.x86_64 < 0.3.14-2.30.11\n" "libpackagekit-glib10-devel.x86_64 < 0.3.14-2.30.11\n" "libpackagekit-qt10.x86_64 < 0.3.14-2.30.11\n" "libpackagekit-qt10-devel.x86_64 < 0.3.14-2.30.11\n" "libsatsolver-devel.x86_64 < 0.17.9-0.5.2\n" "libzypp-devel.x86_64 < 9.38.4-0.7.10\n" "popt-devel.x86_64 < 1.7-37.63.64.1\n" "rpm-32bit.x86_64 < 4.4.2.3-37.63.64.1\n" "rpm-devel.x86_64 < 4.4.2.3-37.63.64.1\n" "ruby-satsolver.x86_64 < 0.44.5-0.5.194\n" "yast2-pkg-bindings-devel-doc.noarch < 2.17.59.2-0.8.13\n" "Loading repository data...\n" "Reading installed packages...\n") subst = "\\g<name> \\g<num> \\g<date>\\n" # You can manually specify the number of replacements by changing the 4th argument result = re.sub(regex, subst, test_str, 0, re.DOTALL | re.IGNORECASE) if result: print (result) # 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