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

r"
"
g

Test String

Code Generator

Generated Code

import Foundation let pattern = #"(\w+)(?:(?:@)|(?:\s+[Aa][Tt]\s+))(.+)(?:(?:\.edu)|(?:\s+[dD][oO][Tt]\s+edu))"# let regex = try! NSRegularExpression(pattern: pattern) let testString = #""" ashishg e ashishg@stanford.edu ashishg e rozm@stanford.edu ashishg p 650-723-1614 ashishg p 650-723-4173 ashishg p 650-814-1478 balaji e balaji@stanford.edu bgirod p 650-723-4539 bgirod p 650-724-3648 bgirod p 650-724-6354 cheriton e cheriton@cs.stanford.edu cheriton e uma@cs.stanford.edu cheriton p 650-723-1131 cheriton p 650-725-3726 dabo e dabo@cs.stanford.edu dabo p 650-725-3897 dabo p 650-725-4671 dlwh e dlwh@stanford.edu engler e engler@lcs.mit.edu engler e engler@stanford.edu eroberts e eroberts@cs.stanford.edu eroberts p 650-723-3642 eroberts p 650-723-6092 fedkiw e fedkiw@cs.stanford.edu hager e hager@cs.jhu.edu hager p 410-516-5521 hager p 410-516-5553 hager p 410-516-8000 hanrahan e hanrahan@cs.stanford.edu hanrahan p 650-723-0033 hanrahan p 650-723-8530 horowitz p 650-725-3707 horowitz p 650-725-6949 jks e jks@robotics.stanford.edu jurafsky e jurafsky@stanford.edu jurafsky p 650-723-5666 kosecka e kosecka@cs.gmu.edu kosecka p 703-993-1710 kosecka p 703-993-1876 kunle e darlene@csl.stanford.edu kunle e kunle@ogun.stanford.edu kunle p 650-723-1430 kunle p 650-725-3713 kunle p 650-725-6949 lam e lam@cs.stanford.edu lam p 650-725-3714 lam p 650-725-6949 latombe e asandra@cs.stanford.edu latombe e latombe@cs.stanford.edu latombe e liliana@cs.stanford.edu latombe p 650-721-6625 latombe p 650-723-0350 latombe p 650-723-4137 latombe p 650-725-1449 levoy e ada@graphics.stanford.edu levoy e melissa@graphics.stanford.edu levoy p 650-723-0033 levoy p 650-724-6865 levoy p 650-725-3724 levoy p 650-725-4089 manning e dbarros@cs.stanford.edu manning e manning@cs.stanford.edu manning p 650-723-7683 manning p 650-725-1449 manning p 650-725-3358 nass e nass@stanford.edu nass p 650-723-5499 nass p 650-725-2472 nick e nick.parlante@cs.stanford.edu nick p 650-725-4727 ok p 650-723-9753 ok p 650-725-1449 ouster e teresa.lynn@stanford.edu ouster e ouster@cs.stanford.edu pal e pal@cs.stanford.edu pal p 650-725-9046 psyoung e patrick.young@stanford.edu rajeev p 650-723-4377 rajeev p 650-723-6045 rajeev p 650-725-4671 rinard e rinard@lcs.mit.edu rinard p 617-253-1221 rinard p 617-258-6922 serafim e serafim@cs.stanford.edu serafim p 650-723-3334 serafim p 650-725-1449 shoham e shoham@stanford.edu shoham p 650-723-3432 shoham p 650-725-1449 subh e subh@stanford.edu subh e uma@cs.stanford.edu subh p 650-724-1915 subh p 650-725-3726 subh p 650-725-6949 thm e pkrokel@stanford.edu thm p 650-725-3383 thm p 650-725-3636 thm p 650-725-3938 tim p 650-724-9147 tim p 650-725-2340 tim p 650-725-4671 ullman e support@gradiance.com ullman e ullman@cs.stanford.edu ullman p 650-494-8016 ullman p 650-725-2588 ullman p 650-725-4802 vladlen e vladlen@stanford.edu widom e siroker@cs.stanford.edu widom e widom@cs.stanford.edu widom p 650-723-0872 widom p 650-723-7690 widom p 650-725-2588 zelenski e zelenski@cs.stanford.edu zelenski p 650-723-6092 zelenski p 650-725-8596 zm e manna@cs.stanford.edu zm p 650-723-4364 zm p 650-725-4671 650-725-4671 650-725-4671 6103564533 (610)3564533 (610) 356-4533 610.405.0420 6104050420 16104050420 610.405-0420 610.405-0420 """# 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