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 = #"[0-9. ]+([a-z.-]+).*\[(.*)\][\s"A-Z]*(/.*) HTTP/[12].[012]""# let regex = try! NSRegularExpression(pattern: pattern) let testString = #""" 10.101.2.114 www.voyage-prive.com - 94.23.212.183 - - [11/May/2017:06:25:39 +0200] "GET /thematique/ventes/207761?from=feaee02fa292afd71911a4c63d2ed692&utm_medium=email&utm_source=NL&utm_campaign=standard&utm_term=NL2&utm_content=110517NLS20NL2VP_FR&ectrans=1 HTTP/1.1" 200 11898 "-" "(varnish-web)" - - 1.083ms 10.101.2.114 www.voyage-prive.com - 78.200.34.180 - - [11/May/2017:06:25:39 +0200] "GET /fiche-produit/details/207749/b1 HTTP/1.1" 200 32128 "http://www.voyage-prive.com/thematique/ventes" "Mozilla/5.0 (Linux; U; Android 4.4.2; fr-fr; GT-P5210 Build/KOT49H) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30 (varnish-web)" - - 0.085ms 10.101.2.114 www.voyage-prive.com - 80.215.46.226 - - [11/May/2017:06:25:39 +0200] "GET /fiche-produit/details/208003 HTTP/1.1" 200 44186 "-" "Mozilla/5.0 (iPhone; CPU iPhone OS 10_0_2 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) Mobile/14A456 (varnish-web)" - - 0.051ms 10.101.2.114 www.voyage-prive.com - 89.80.18.134 - - [11/May/2017:06:25:39 +0200] "GET /search/doAjaxSearch?sid=208088&decode=true&version=2&with_dispos=1&culture=fr_FR&oid=3602628&with_catalog=true&papaya2=0 HTTP/1.1" 200 18690 "http://www.voyage-prive.com/fiche-produit/details/208088/b1" "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1 (varnish-web)" - - 0.014ms 10.101.2.114 www.voyage-prive.com - 89.80.18.134 - - [11/May/2017:06:25:40 +0200] "GET /search/doAjaxSearch?sid=208088&decode=true&version=2&with_dispos=1&culture=fr_FR&oid=3602629&with_catalog=true&papaya2=0 HTTP/1.1" 200 3759 "http://www.voyage-prive.com/fiche-produit/details/208088/b1" "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1 (varnish-web)" - - 0.008ms 10.101.2.114 www.voyage-prive.com - 81.57.89.166 - - [11/May/2017:06:25:40 +0200] "GET /fiche-produit/details/207756 HTTP/1.1" 200 42503 "-" "Mozilla/5.0 (iPad; CPU OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Mobile/14E304 (varnish-web)" - - 0.045ms 10.101.2.114 www.voyage-prive.com - 212.83.158.193 - - [11/May/2017:06:25:40 +0200] "GET /login/index HTTP/1.1" 200 9555 "-" "Python-urllib/2.6 (varnish-web)" - - 0.054ms 10.101.2.114 www.voyage-prive.com - 10.110.2.254 - - [11/May/2017:06:25:40 +0200] "GET /api/member/update?id_membre=5295731&culture=fr_FR HTTP/1.1" 200 1387 "-" "Phalcon HTTP/0.0.2 (Curl) (varnish-web)" - - 0.023ms 10.101.2.114 www.voyage-prive.it - 212.83.158.193 - - [11/May/2017:06:25:40 +0200] "GET /login/index HTTP/1.1" 200 9042 "-" "Python-urllib/2.6 (varnish-web)" - - 0.052ms 10.101.2.114 www.voyage-prive.com - 78.118.109.58 - - [11/May/2017:06:25:41 +0200] "GET /search/doAjaxMerchSdv?d[]=eu.fr.fr11.par&is_admin=0&sales_type=filtered&list=2&version=3&groupBy=isOnline&count=250&page=1&culture=fr_FR&onlySale=1 HTTP/1.1" 200 15847 "http://www.voyage-prive.com/thematique/ventes" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/602.2.14 (KHTML, like Gecko) Version/10.0.1 Safari/602.2.14 (varnish-web)" - - 0.011ms """# 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