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

Test String

Code Generator

Generated Code

import Foundation let pattern = #"Command\s+(\w+)\s+returned:\s+(.*)"# let regex = try! NSRegularExpression(pattern: pattern, options: [.anchorsMatchLines, .dotMatchesLineSeparators]) let testString = #""" Command google returned: [ { "title": "Cloning a repository - GitHub Docs", "href": "https://docs.github.com/en/repositories/creating-and-managing-repositories/cloning-a-repository", "body": "Cloning a repository. On GitHub.com, navigate to the main page of the repository. Above the list of files, click Code . Copy the URL for the repository. To clone the repository using HTTPS, under \"HTTPS\", click . To clone the repository using an SSH key, including a certificate issued by your organization's SSH certificate authority, click SSH ..." }, { "title": "How to Clone a GitHub Repository - How-To Geek", "href": "https://www.howtogeek.com/451360/how-to-clone-a-github-repository/", "body": "Here's how to clone a GitHub repository. The first thing you'll need to do is download and install Git on your computer. The installation process is straightforward and brings you through a lot of boilerplate information. The one thing you want to be careful with is that you allow Git to be used from the command line." }, { "title": "Git - git-clone Documentation", "href": "https://www.git-scm.com/docs/git-clone", "body": "Reject to clone a repository if it is a shallow one, can be overridden by passing option --reject-shallow in command line. See git-clone[1] clone.filterSubmodules . If a partial clone filter is provided (see --filter in git-rev-list[1]) and --recurse-submodules is used, also apply the filter to submodules." }, { "title": "git clone | Atlassian Git Tutorial", "href": "https://www.atlassian.com/git/tutorials/setting-up-a-repository/git-clone", "body": "git clone is primarily used to point to an existing repo and make a clone or copy of that repo at in a new directory, at another location. The original repository can be located on the local filesystem or on remote machine accessible supported protocols. The git clone command copies an existing Git repository. This is sort of like SVN checkout ..." }, { "title": "How To Clone a Git Repository - devconnected", "href": "https://devconnected.com/how-to-clone-a-git-repository/", "body": "In order to clone a git repository into a specific folder, execute the \"git clone\" command and specify the destination folder at the end. $ git clone <url> <directory>. For example, given the Github project we fetched in the previous section, if we want to clone it into a folder named \"myproject\" we would run." }, { "title": "How do I clone a Git repository into a specific folder?", "href": "https://stackoverflow.com/questions/651038/how-do-i-clone-a-git-repository-into-a-specific-folder", "body": "Add a comment. -2. For Windows user 1> Open command prompt. 2> Change the directory to destination folder (Where you want to store your project in local machine.) 3> Now go to project setting online (From where you want to clone) 4> Click on clone, and copy the clone command. 5> Now enter the same on cmd ." }, { "title": "Clone a repository | Bitbucket Cloud | Atlassian Support", "href": "https://support.atlassian.com/bitbucket-cloud/docs/clone-a-repository/", "body": "These instructions show you how to clone your repository using Git from the terminal. In the repository, select the Clone button. Copy the clone command. From a terminal window, change into the local directory where you want to clone your repository. $ cd <path_to_directory> Paste the command you copied from Bitbucket, for example:" }, { "title": "How to clone a git repository using the command line", "href": "https://www.educative.io/answers/how-to-clone-a-git-repository-using-the-command-line", "body": "This particular article will focus on cloning a repository using a command line. Follow the directions below: Open GitHub and go to the GitHub repository that you want to clone. Click \"Code\" and copy the given URL. click code and copy the URL. Open \"Git Bash\" and change the current working directory to the location where you want the ..." } ] """# 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