Regular Expressions 101

Save & Share

  • Regex Version: ver. 2
  • Update Regex
    ctrl+⇧+s
  • Save new Regex
    ctrl+s
  • Add to Community Library

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
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"addr=\"(([0-9]{1,3}[\\.]){3}[0-9]{1,3})\"" test_str = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" "<!DOCTYPE nmaprun>\n" "<?xml-stylesheet href=\"file:///usr/bin/../share/nmap/nmap.xsl\" type=\"text/xsl\"?>\n" "<!-- Nmap 7.80 scan initiated Fri Oct 16 04:52:18 2020 as: nmap -O -oX xml_data.xml 45.33.49.119 -->\n" "<nmaprun scanner=\"nmap\" args=\"nmap -O -oX xml_data.xml 45.33.49.119\" start=\"1602823938\" startstr=\"Fri Oct 16 04:52:18 2020\" version=\"7.80\" xmloutputversion=\"1.04\">\n" "<scaninfo type=\"syn\" protocol=\"tcp\" numservices=\"1000\" services=\"1,3-4,6-7,9,13,17,19-26,30,32-33,37,42-43,49,53,70,79-85,88-90,99-100,106,109-111,113,119,125,135,139,143-144,146,161,163,179,199,211-212,22>\n" "<verbose level=\"0\"/>\n" "<debugging level=\"0\"/>\n" "<host starttime=\"1602823938\" endtime=\"1602823972\"><status state=\"up\" reason=\"reset\" reason_ttl=\"64\"/>\n" "<address addr=\"45.33.49.119\" addrtype=\"ipv4\"/>\n" "<hostnames>\n" "<hostname name=\"ack.nmap.org\" type=\"PTR\"/>\n" "</hostnames>\n" "<ports><extraports state=\"filtered\" count=\"993\">\n" "<extrareasons reason=\"no-responses\" count=\"993\"/>\n" "</extraports>\n" "<port protocol=\"tcp\" portid=\"22\"><state state=\"open\" reason=\"syn-ack\" reason_ttl=\"51\"/><service name=\"ssh\" method=\"table\" conf=\"3\"/></port>\n" "<port protocol=\"tcp\" portid=\"25\"><state state=\"open\" reason=\"syn-ack\" reason_ttl=\"51\"/><service name=\"smtp\" method=\"table\" conf=\"3\"/></port>\n" "<port protocol=\"tcp\" portid=\"70\"><state state=\"closed\" reason=\"reset\" reason_ttl=\"51\"/><service name=\"gopher\" method=\"table\" conf=\"3\"/></port>\n" "<port protocol=\"tcp\" portid=\"80\"><state state=\"open\" reason=\"syn-ack\" reason_ttl=\"50\"/><service name=\"http\" method=\"table\" conf=\"3\"/></port>\n" "<port protocol=\"tcp\" portid=\"113\"><state state=\"closed\" reason=\"reset\" reason_ttl=\"51\"/><service name=\"ident\" method=\"table\" conf=\"3\"/></port>\n" "<port protocol=\"tcp\" portid=\"443\"><state state=\"open\" reason=\"syn-ack\" reason_ttl=\"50\"/><service name=\"https\" method=\"table\" conf=\"3\"/></port>\n" "<port protocol=\"tcp\" portid=\"31337\"><state state=\"closed\" reason=\"reset\" reason_ttl=\"51\"/><service name=\"Elite\" method=\"table\" conf=\"3\"/></port>\n" "</ports>\n" "<os><portused state=\"open\" proto=\"tcp\" portid=\"22\"/>\n" "<portused state=\"closed\" proto=\"tcp\" portid=\"70\"/>\n" "<osmatch name=\"HP P2000 G3 NAS device\" accuracy=\"91\" line=\"34647\">\n" "<osclass type=\"storage-misc\" vendor=\"HP\" osfamily=\"embedded\" accuracy=\"91\"><cpe>cpe:/h:hp:p2000_g3</cpe></osclass>\n" "</osmatch>\n" "<osmatch name=\"Linux 2.6.32\" accuracy=\"90\" line=\"55409\">\n" "<osclass type=\"general purpose\" vendor=\"Linux\" osfamily=\"Linux\" osgen=\"2.6.X\" accuracy=\"90\"><cpe>cpe:/o:linux:linux_kernel:2.6.32</cpe></osclass>\n" "</osmatch>\n" "<osmatch name=\"Linux 2.6.32 - 3.1\" accuracy=\"90\" line=\"56315\">\n" "<osclass type=\"general purpose\" vendor=\"Linux\" osfamily=\"Linux\" osgen=\"2.6.X\" accuracy=\"90\"><cpe>cpe:/o:linux:linux_kernel:2.6</cpe></osclass>\n" "<osclass type=\"general purpose\" vendor=\"Linux\" osfamily=\"Linux\" osgen=\"3.X\" accuracy=\"90\"><cpe>cpe:/o:linux:linux_kernel:3</cpe></osclass>\n" "</osmatch>\n" "<osmatch name=\"Ubiquiti AirMax NanoStation WAP (Linux 2.6.32)\" accuracy=\"90\" line=\"61488\">\n" "<osclass type=\"WAP\" vendor=\"Linux\" osfamily=\"Linux\" osgen=\"2.6.X\" accuracy=\"90\"><cpe>cpe:/o:linux:linux_kernel:2.6.32</cpe></osclass>\n" "<osclass type=\"WAP\" vendor=\"Ubiquiti\" osfamily=\"embedded\" accuracy=\"90\"><cpe>cpe:/h:ubnt:airmax_nanostation</cpe></osclass>\n" "</osmatch>\n" "<osmatch name=\"Linux 3.7\" accuracy=\"90\" line=\"65676\">\n" "<osclass type=\"general purpose\" vendor=\"Linux\" osfamily=\"Linux\" osgen=\"3.X\" accuracy=\"90\"><cpe>cpe:/o:linux:linux_kernel:3.7</cpe></osclass>\n" "</osmatch>\n" "<osmatch name=\"Linux 2.6.32 - 3.13\" accuracy=\"89\" line=\"56411\">\n" "<osclass type=\"general purpose\" vendor=\"Linux\" osfamily=\"Linux\" osgen=\"2.6.X\" accuracy=\"89\"><cpe>cpe:/o:linux:linux_kernel:2.6</cpe></osclass>\n" "<osclass type=\"general purpose\" vendor=\"Linux\" osfamily=\"Linux\" osgen=\"3.X\" accuracy=\"89\"><cpe>cpe:/o:linux:linux_kernel:3</cpe></osclass>\n" "</osmatch>\n" "<osmatch name=\"Linux 3.0 - 3.2\" accuracy=\"89\" line=\"62456\">\n" "<osclass type=\"general purpose\" vendor=\"Linux\" osfamily=\"Linux\" osgen=\"3.X\" accuracy=\"89\"><cpe>cpe:/o:linux:linux_kernel:3</cpe></osclass>\n" "</osmatch>\n" "<osmatch name=\"Linux 3.3\" accuracy=\"89\" line=\"65197\">\n" "<osclass type=\"general purpose\" vendor=\"Linux\" osfamily=\"Linux\" osgen=\"3.X\" accuracy=\"89\"><cpe>cpe:/o:linux:linux_kernel:3.3</cpe></osclass>\n" "</osmatch>\n" "<osmatch name=\"Infomir MAG-250 set-top box\" accuracy=\"89\" line=\"59437\">\n" "<osclass type=\"media device\" vendor=\"Linux\" osfamily=\"Linux\" osgen=\"2.6.X\" accuracy=\"89\"><cpe>cpe:/o:linux:linux_kernel:2.6</cpe></osclass>\n" "<osclass type=\"media device\" vendor=\"Infomir\" osfamily=\"embedded\" accuracy=\"89\"><cpe>cpe:/h:infomir:mag-250</cpe></osclass>\n" "</osmatch>\n" "<osmatch name=\"Ubiquiti Pico Station WAP (AirOS 5.2.6)\" accuracy=\"88\" line=\"102825\">\n" "<osclass type=\"WAP\" vendor=\"Ubiquiti\" osfamily=\"AirOS\" osgen=\"5.X\" accuracy=\"88\"><cpe>cpe:/o:ubnt:airos:5.2.6</cpe></osclass>\n" "</osmatch>\n" "</os>\n" "<uptime seconds=\"751775\" lastboot=\"Wed Oct 7 12:03:18 2020\"/>\n" "<tcpsequence index=\"256\" difficulty=\"Good luck!\" values=\"5833A5A,D5206F79,8E66294D,8E3B0632,AC2DB44A,7F486476\"/>\n" "<ipidsequence class=\"All zeros\" values=\"0,0,0,0,0,0\"/>\n" "<tcptssequence class=\"1000HZ\" values=\"2CCF1F79,2CCF1FD4,2CCF2043,2CCF209E,2CCF2102,2CCF2164\"/>\n" "<times srtt=\"193340\" rttvar=\"78390\" to=\"506900\"/>\n" "</host>\n" "<runstats><finished time=\"1602823973\" timestr=\"Fri Oct 16 04:52:53 2020\" elapsed=\"34.33\" summary=\"Nmap done at Fri Oct 16 04:52:53 2020; 1 IP address (1 host up) scanned in 34.33 seconds\" exit=\"success\"/><>\n" "</runstats>\n" "</nmaprun>") 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