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"
"
gmux

Test String

Code Generator

Generated Code

import Foundation // WARNING: You included a flag that Swift doesn't support: u // When this flag is set, it makes the pattern and subject strings to be treated as unicode. // Swift already treats the pattern and subject strings as unicode by default, so including this flag is redundant. let pattern = #""" ^ (?: [ \t]*[(][ \t]* (\d+) [ \t]*,[ \t]* (\d+) [ \t]*,[ \t]* (\S+) [ \t]*[)][ \t]* )? $ """# let regex = try! NSRegularExpression(pattern: pattern, options: [.anchorsMatchLines, .allowCommentsAndWhitespace]) let testString = #""" ( 0, 12, Tokenization ) ( 13 , 15 , is ) ( 16, 22, widely) ( 23, 31, regarded ) (32, 34, as ) (35, 36, a) (37, 43, solved ) (44, 51, problem) (52, 55, due) (56, 58, to) (59, 62, the) (63, 67, high) (68, 76, accuracy) (77, 81, that) (82, 91, rulebased) (92, 102, tokenizers) (103, 110, achieve) (110, 111, .) (0, 3, But) (4, 14, rule-based) (15, 25, tokenizers) (26, 29, are) (30, 34, hard) (35, 37, to) (38, 46, maintain) (47, 50, and) (51, 56, their) (57, 62, rules) (63, 71, language) (72, 80, specific) (80, 81, .) (0, 2, We) (3, 7, show) (8, 12, that) (13, 17, high) (18, 26, accuracy) (27, 31, word) (32, 35, and) (36, 44, sentence) (45, 57, segmentation) (58, 61, can) (62, 64, be) (65, 73, achieved) (74, 76, by) (77, 82, using) (83, 93, supervised) (94, 102, sequence) (103, 111, labeling) (112, 114, on) (115, 118, the) (119, 128, character) (129, 134, level) (135, 143, combined) (144, 148, with) (149, 161, unsupervised) (162, 169, feature) (170, 178, learning) (178, 179, .) (0, 2, We) (3, 12, evaluated) (13, 16, our) (17, 23, method) (24, 26, on) (27, 32, three) (33, 42, languages) (43, 46, and) (47, 55, obtained) (56, 61, error) (62, 67, rates) (68, 70, of) (71, 75, 0.27) (76, 77, ‰) (78, 79, () (79, 86, English) (86, 87, )) (87, 88, ,) (89, 93, 0.35) (94, 95, ‰) (96, 97, ( ) (97, 102, Dutch) (102, 103, ) ) (104, 107, and) (108, 112, 0.76) (113, 114, ‰) (115, 116, () (116, 123, Italian) (123, 124, )) (125, 128, for) (129, 132, our) (133, 137, best) (138, 144, models) (144, 145, .) """# 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