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

/
/
g

Test String

Code Generator

Generated Code

import Foundation let pattern = #"(%([^:]+):([^%]+)%)"# let regex = try! NSRegularExpression(pattern: pattern) let testString = ##""" <!DOCTYPE html>/ <html> <head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /> <title>Testing the checkAndChangeToUrl-Function (Version 5)</title> <link rel=\"stylesheet\" type=\"text/css\" href=\"[__protocol__]://[__server__]:[__port__]/weblis-ext3/ext/resources/css/ext-all.css\" /> <script type=\"text/javascript\" src=\"[__protocol__]://[__server__]:[__port__]/weblis-ext3/ext/adapter/ext/ext-base-debug.js\"></script> <script type=\"text/javascript\" src=\"[__protocol__]://[__server__]:[__port__]/weblis-ext3/ext/ext-all-debug.js\"></script> <script type=\"text/javascript\" src=\"[__protocol__]://[__server__]:[__port__]/weblis-ext3/ext/locale/ext-lang-de.js\"></script> <script type=\"text/javascript\" src=\"[__protocol__]://[__server__]:[__port__]/weblis-resources/js/jquery/jquery-3.2.1.js\"></script> <script type=\"text/javascript\"> function checkAndChangeToUrl(url, maxRetries, waitTime, waitFunc, startFunc, successFunc, errorFunc) { console.log(\"ENTER checkAndChangeToUrl('\"+url+\"', \"+maxRetries+\", \"+waitTime+\")\"); var nRetry = 0; var checkUrl = function () { $.ajax({ url: url, context: document.body, crossDomain: true, error: function(jqXHR, textStatus, errorThrown) { console.error(textStatus + \"; \" + errorThrown); nRetry++; if (nRetry <= maxRetries) { waitFunc.call(jqXHR, jqXHR); console.log(\"Retry #\" + nRetry); var timeout = (nRetry == 0 ? 0 : waitTime); console.log(\"timeout = \" + timeout); setTimeout(checkUrl, timeout); if (nRetry === 1) { console.log(\"**** Try and open the target application ****\"); // PROBLEM: parent[.parent].handleExternalEvent() existiert nicht... startFunc.call(jqXHR, jqXHR); } } else { errorFunc.call(jqXHR, jqXHR); } return; }, success : function(data, textStatus, jqXHR) { console.log(\"Success!\"); successFunc.call(jqXHR, jqXHR); } }).done(function() { $( this ).addClass( \"done\" ); console.log(\"Done!\"); }); }; // Wir setzen das Ganze in Gang... checkUrl(); } function test_1(checkUrl, targetUrl, launchUrl) { //var url = \"%param:checkUrl%\"; //var url = \"http://localhost:8080/RechercheServlet/rest/forcont/fxLIS?DOKID=1&VID=1&OP=RECEIVE\"; //var url = \"http://localhost:8085/gn\"; console.log(\"VOR checkAndChangeToUrl('\"+checkUrl+\"', 2, 5000) ...\"); var ret = checkAndChangeToUrl(checkUrl, 2, 2000, function(jqXHR) { console.log(\"Waiting...\"); }, function(jqXHR) { console.log(\"Start application...\"); $.ajax({ url: %param:launchUrl%, //\"/opusp-rest/client-action/execute/local/GIS_Launch\", context: document.body, crossDomain: false, //true, -- SYNCHRONER AUFRUF - SCHWERE SÜNDE... headers: { 'Accept' : 'application/x-java-jnlp-file', /* application/jnlp */ 'Content-Description' : 'File Transfer', 'Content-Disposition' : 'attachment; filename=GIS_Launch.jnlp' }, error: function(jqXHR, textStatus, errorThrown) { console.error(\"Fehler bei Aktion GIS_Launch: \" + textStatus + \"; \" + errorThrown); return; }, success : function(data, textStatus, jqXHR) { console.log(\"GIS_Launch: Success! JNLP = '\" + data + \"'\"); } }).done(function() { $( this ).addClass( \"done\" ); console.log(\"GIS_Launch: Done!\"); }); }, function(jqXHR) { console.log(\"Success.\"); window.location.href = targetUrl; }, function(jqXHR) { console.log(\"Error: \" + jqXHR.statusText + \", jqXHR.state() = \" + jqXHR.state()); }); console.log(\"LEAVE test_1.\"); } function performTest_1() { test_1( \"[__checkUrl__]\", // Parameter: checkUrl, z.B. \"http://localhost:8080/RechercheServlet\" \"%param:targetUrl%\", // Parameter: targetUrl, z.B. \"http://localhost:8080/RechercheServlet/rest/forcont/fxLIS?DOKID=1&VID=1&OP=RECEIVE\" \"[__launchUrl__]\" // Parameter: launchUrl, z.B. \"/opusp-rest/client-action/execute/local/GIS_Launch\" ) } $.when($.ready).then(function() { performTest_1(); }); </script> </head> <body> <a id=\"test_1\" onclick=\"performTest_1();\"><button type=\"button\">Perform Test #1</button></a> </body> </html> """## 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