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

import Foundation let pattern = #"addr=\"(([0-9]{1,3}[\\.]){3}[0-9]{1,3})\""# let regex = try! NSRegularExpression(pattern: pattern) let testString = #""" <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE nmaprun> <?xml-stylesheet href="file:///usr/bin/../share/nmap/nmap.xsl" type="text/xsl"?> <!-- Nmap 7.80 scan initiated Fri Oct 16 04:52:18 2020 as: nmap -O -oX xml_data.xml 45.33.49.119 --> <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"> <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> <verbose level="0"/> <debugging level="0"/> <host starttime="1602823938" endtime="1602823972"><status state="up" reason="reset" reason_ttl="64"/> <address addr="45.33.49.119" addrtype="ipv4"/> <hostnames> <hostname name="ack.nmap.org" type="PTR"/> </hostnames> <ports><extraports state="filtered" count="993"> <extrareasons reason="no-responses" count="993"/> </extraports> <port protocol="tcp" portid="22"><state state="open" reason="syn-ack" reason_ttl="51"/><service name="ssh" method="table" conf="3"/></port> <port protocol="tcp" portid="25"><state state="open" reason="syn-ack" reason_ttl="51"/><service name="smtp" method="table" conf="3"/></port> <port protocol="tcp" portid="70"><state state="closed" reason="reset" reason_ttl="51"/><service name="gopher" method="table" conf="3"/></port> <port protocol="tcp" portid="80"><state state="open" reason="syn-ack" reason_ttl="50"/><service name="http" method="table" conf="3"/></port> <port protocol="tcp" portid="113"><state state="closed" reason="reset" reason_ttl="51"/><service name="ident" method="table" conf="3"/></port> <port protocol="tcp" portid="443"><state state="open" reason="syn-ack" reason_ttl="50"/><service name="https" method="table" conf="3"/></port> <port protocol="tcp" portid="31337"><state state="closed" reason="reset" reason_ttl="51"/><service name="Elite" method="table" conf="3"/></port> </ports> <os><portused state="open" proto="tcp" portid="22"/> <portused state="closed" proto="tcp" portid="70"/> <osmatch name="HP P2000 G3 NAS device" accuracy="91" line="34647"> <osclass type="storage-misc" vendor="HP" osfamily="embedded" accuracy="91"><cpe>cpe:/h:hp:p2000_g3</cpe></osclass> </osmatch> <osmatch name="Linux 2.6.32" accuracy="90" line="55409"> <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> </osmatch> <osmatch name="Linux 2.6.32 - 3.1" accuracy="90" line="56315"> <osclass type="general purpose" vendor="Linux" osfamily="Linux" osgen="2.6.X" accuracy="90"><cpe>cpe:/o:linux:linux_kernel:2.6</cpe></osclass> <osclass type="general purpose" vendor="Linux" osfamily="Linux" osgen="3.X" accuracy="90"><cpe>cpe:/o:linux:linux_kernel:3</cpe></osclass> </osmatch> <osmatch name="Ubiquiti AirMax NanoStation WAP (Linux 2.6.32)" accuracy="90" line="61488"> <osclass type="WAP" vendor="Linux" osfamily="Linux" osgen="2.6.X" accuracy="90"><cpe>cpe:/o:linux:linux_kernel:2.6.32</cpe></osclass> <osclass type="WAP" vendor="Ubiquiti" osfamily="embedded" accuracy="90"><cpe>cpe:/h:ubnt:airmax_nanostation</cpe></osclass> </osmatch> <osmatch name="Linux 3.7" accuracy="90" line="65676"> <osclass type="general purpose" vendor="Linux" osfamily="Linux" osgen="3.X" accuracy="90"><cpe>cpe:/o:linux:linux_kernel:3.7</cpe></osclass> </osmatch> <osmatch name="Linux 2.6.32 - 3.13" accuracy="89" line="56411"> <osclass type="general purpose" vendor="Linux" osfamily="Linux" osgen="2.6.X" accuracy="89"><cpe>cpe:/o:linux:linux_kernel:2.6</cpe></osclass> <osclass type="general purpose" vendor="Linux" osfamily="Linux" osgen="3.X" accuracy="89"><cpe>cpe:/o:linux:linux_kernel:3</cpe></osclass> </osmatch> <osmatch name="Linux 3.0 - 3.2" accuracy="89" line="62456"> <osclass type="general purpose" vendor="Linux" osfamily="Linux" osgen="3.X" accuracy="89"><cpe>cpe:/o:linux:linux_kernel:3</cpe></osclass> </osmatch> <osmatch name="Linux 3.3" accuracy="89" line="65197"> <osclass type="general purpose" vendor="Linux" osfamily="Linux" osgen="3.X" accuracy="89"><cpe>cpe:/o:linux:linux_kernel:3.3</cpe></osclass> </osmatch> <osmatch name="Infomir MAG-250 set-top box" accuracy="89" line="59437"> <osclass type="media device" vendor="Linux" osfamily="Linux" osgen="2.6.X" accuracy="89"><cpe>cpe:/o:linux:linux_kernel:2.6</cpe></osclass> <osclass type="media device" vendor="Infomir" osfamily="embedded" accuracy="89"><cpe>cpe:/h:infomir:mag-250</cpe></osclass> </osmatch> <osmatch name="Ubiquiti Pico Station WAP (AirOS 5.2.6)" accuracy="88" line="102825"> <osclass type="WAP" vendor="Ubiquiti" osfamily="AirOS" osgen="5.X" accuracy="88"><cpe>cpe:/o:ubnt:airos:5.2.6</cpe></osclass> </osmatch> </os> <uptime seconds="751775" lastboot="Wed Oct 7 12:03:18 2020"/> <tcpsequence index="256" difficulty="Good luck!" values="5833A5A,D5206F79,8E66294D,8E3B0632,AC2DB44A,7F486476"/> <ipidsequence class="All zeros" values="0,0,0,0,0,0"/> <tcptssequence class="1000HZ" values="2CCF1F79,2CCF1FD4,2CCF2043,2CCF209E,2CCF2102,2CCF2164"/> <times srtt="193340" rttvar="78390" to="506900"/> </host> <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"/><> </runstats> </nmaprun> """# let stringRange = NSRange(location: 0, length: testString.utf16.count) let matches = regex.matches(in: testString, range: stringRange) var result: [[String]] = [] for match in matches { var groups: [String] = [] for rangeIndex in 1 ..< match.numberOfRanges { let nsRange = match.range(at: rangeIndex) guard !NSEqualRanges(nsRange, NSMakeRange(NSNotFound, 0)) else { continue } let string = (testString as NSString).substring(with: nsRange) groups.append(string) } if !groups.isEmpty { result.append(groups) } } print(result)

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 Swift 5.2, please visit: https://developer.apple.com/documentation/foundation/nsregularexpression