Regular Expressions 101

Save & Share

  • Regex Version: ver. 5
  • 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

/
/
gi

Test String

Code Generator

Generated Code

import Foundation let pattern = #"((?![.\n\s])[^.\n"]*(?:"[^\n"]*[^\n".]"[^.\n"]*)*(?:"[^"\n]+\."|\.|(?=\n)))"# let regex = try! NSRegularExpression(pattern: pattern, options: .caseInsensitive) let testString = #""" She was reading her little lord a tale of the Winged Knight when Mya Stone came knocking on the door of his bedchamber, clad in boots and riding leathers and smelling strongly of the stable. Mya had straw in her hair and a scowl on her face. That scowl comes of having Mychel Redfort near, Alayne knew "Your lordship," Mya informed Lord Robert, "Lady Waynwood’s banners have been seen an hour down the road. She will be here soon, with your cousin Harry. Will you want to greet them" Why did she have to mention Harry. Alayne thought. We will never get Sweetrobin out of bed now. The boy slapped a pillow. "Send them away. I never asked them here." Mya looked nonplussed. No one in the Vale was better at handling a mule, but lordlings were another matter. "They were invited," she said uncertainly, "for the tourney. I don’t..." Alayne closed her book. "Thank you, Mya. Let me talk with Lord Robert, if you would." Relief plain on her face, Mya fled without another word. "I hate that Harry," Sweetrobin said when she was gone. "He calls me cousin, but he’s just waiting for me to die so he can take the Eyrie. He thinks I don’t know, but I do." "Your lordship should not believe such nonsense," Alayne said. "I’m sure Ser Harrold loves you well." And if the gods are good, he will love me too. Her tummy gave a little flutter. "He doesn’t," Lord Robert insisted. "He wants my father’s castle, that’s all, so he pretends." The boy clutched the blanket to his pimply chest. "I don’t want you to marry him, Alayne. I am the Lord of the Eyrie, and I forbid it." He sounded as if he were about to cry. "You should marry me instead. We could sleep in the same bed every night, and you could read me stories." No man can wed me so long as my dwarf husband still lives somewhere in this world. Queen Cersei had collected the head of a dozen dwarfs, Petyr claimed, but none were Tyrion’s. "Sweetrobin, you must not say such things. You are the Lord of the Eyrie and Defender of the Vale, and you must wed a highborn lady and father a son to sit in the High Hall of House Arryn after you are gone." """# 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