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
No Match

/
/

Test String

Code Generator

Generated Code

import Foundation let pattern = #"(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)"# let regex = try! NSRegularExpression(pattern: pattern) let testString = #"1.3626104100298,44.0313777738606,0 -121.3627743244628,44.03123252081081,0 -121.3627794325398,44.03122812311533,0 -121.3627843389654,44.03122393122449,0 -121.3627892667884,44.0312197526747,0 -121.3627942147613,44.0312155874754,0 -121.3627991841185,44.03121143471726,0 -121.3628041748603,44.03120729440029,0 -121.3628091845174,44.03120316834296,0 -121.3628142168063,44.03119905471752,0 -121.3628192704925,44.0311949544332,0 -121.3628243430812,44.03119086750853,0 -121.3628294370801,44.03118679482493,0 -121.362834551216,44.0311827345917,0 -121.3628396867492,44.03117868769964,0 -121.3628448411978,44.03117465506718,0 -121.3628500170308,44.03117063487584,0 -121.3621653794692,44.03071984342947,0 -121.3619768731163,44.03100429635388,0 -121.3626104100298,44.0313777738606,0"# let stringRange = NSRange(location: 0, length: testString.utf16.count) if let firstMatch = regex.firstMatch(in: testString, range: stringRange) { let result: [String] = (1 ..< firstMatch.numberOfRanges).map { (testString as NSString).substring(with: firstMatch.range(at: $0)) } print(result) } else { print("No matches were found.") }

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