Regular Expressions 101

Save & Share

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

/
/
gm

Test String

Code Generator

Generated Code

# coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility import re regex = r"((?<==\h)\([A-Z][A-Z0-9_]+\b(?=[<:\;])|\b(?<!<)[A-Z][A-Z0-9_]+(?=[^>\)]))\b|\b((?<!=\s)(?<!\.)[A-Z]+\d*?(?![\(\;:])(?=[,\)>\s]))\b" test_str = ("This still has a problem with FOUR+ character match \n" "\\b(?<!val\\s)(?<!var\\s)(?<!=)(?<!=\\s)(?<!\\.)([A-Z]{1,3}[0-9]*?)\\b\n" "\\b(?<!val\\s|var\\s|=|=\\s|\\.)([A-Z]+|[A-Z0-9]+)\\b\n" "\\b([A-Z0-9]{1,3})\\b(?![\\(;:])(?=[,\\)> ])\n" "<[A-Z0-9]+>|\\([A-Z0-9]+\\)\n" "\\b([A-Z0-9]{1,3})\\b\n\n" "<in T, out R>\n" ": R {\n" ": R =\n" "(S, T, R)\n" "(S)\n" "-> S\n" "-> (S)\n\n" "[^a-z:\\s\\.\\,]\n\n" "<>\n" ":\\w+(?=,|>)\n\n" "//-------------------\n\n" "class IO<out A>(val fn: () -> A) { // blows the stack\n" " fun <B> map(g: (A) -> B): IO<B> = IO { g(this()) }\n" " fun <B> flatMap(g: (A) -> IO<B>): IO<B> = IO { g(this())() }\n\n" " operator fun invoke(): A = fn()\n\n" " companion object {\n" " val empty: IO<Unit> = IO {}\n" " operator fun <A> invoke(a: A): IO<A> = IO { a }\n" " }\n" "}\n\n" "// blows the stack\n" "fun <A, B> forever(ioa: IO<A>): IO<B> {\n" " val t: () -> IO<B> = { forever(ioa) }\n" " return ioa.flatMap { t() }\n" "}\n\n" "var count = 0\n" "val program: IOU = IO {\n" " forever<String, String>(IO { \"${count++} Repeating function\" })\n" " .flatMap { Console.println(it) }\n" "}\n\n" "//-----------------------\n\n" "class State<S, out T>(val runState: (S) -> Pair<S, T>) {\n\n" " fun <R> map(f: (T) -> R): State<S, R> =\n" " flatMap { t -> pure<S, R>(f(t)) }\n\n" " fun <P15, R> map(sx: State<S, P1>, f: (T, P1) -> R): State<STAB, R> =\n" " flatMap { t -> sx.map { x -> f(t, x) } }\n\n" " fun <R> flatMap(f: (T) -> State<S, R>): State<S, R> =\n" " State { s ->\n" " val (s1, t) = runState(s)\n" " f(t).runState(s1)\n" " }\n\n" " companion object {\n" " fun <S, T> pure(t: T): State<S, T> =\n" " State { s -> s to t }\n\n" " fun <S> get(): State<S, S> =\n" " State { s -> s to s }\n\n" " fun <S> set(newState: S): State<S, Unit> =\n" " State { _ -> newState to Unit }\n\n" " fun <S> modify(f: (S) -> S): State<S, Unit> =\n" " get<S>().flatMap { s: S -> set(f(s)).map { Unit } }\n" " }\n" "}\n\n" "//----------------\n\n" "data class Book(\n" " val ISDN: String,\n" " val name: String,\n" " val pages: Int,\n" " val price: Price,\n" " val weight: Double,\n" " val year: Int,\n" " val author: String\n" ")\n\n" "//-------------------\n\n" "fun <S, A> StateAlias<S, A>.map(f: (A) -> Boolean): StateAlias<S, Boolean> =\n" " { s ->\n" " val (s1, t) = this(s)\n" " when {\n" " f(s1) -> Pair(true, t)\n" " else -> Pair(false, t)\n" " }\n" " }\n\n" "fun <S, A, B> flatMap(): ((StateAlias<S, A>) -> (A)) -> ((A) -> (StateAlias<S, B>)) -> StateAlias<S, B>\n\n" "//--------------------\n\n" "val dog = Dog(id = 0, name = \"Doglin\", furColour = FurColour.BLACK\n\n" "val cat: DOG = DOG\n\n" "//-------------------\n\n" "fun <T : Comparable<T>> sort(list: List<T>)\n\n" "fun <T> cloneWhenGreater(list: List<T>, threshold: T): List<T>\n" " where T : Comparable,\n" " T : Cloneable {\n" " return list.filter { it > threshold }.map { it.clone() }\n" "}\n\n" "//----------------------\n\n" "enum class FurColour { BLACK, GREEN, PATCHED; }\n\n" "enum class Direction {\n" " NORTH, SOUTH, WEST, EAST\n" "}\n\n" "enum class Color(val rgb: Int) {\n" " RED(0xFF0000),\n" " GREEN(0x00FF00),\n" " BLUE(0x0000FF)\n" "}\n\n" "enum class ProtocolState {\n" " WAITING {\n" " override fun signal() = TALKING\n" " },\n\n" " TALKING {\n" " override fun signal() = WAITING\n" " };\n\n" " abstract fun signal(): ProtocolState\n" "}") matches = re.finditer(regex, test_str, re.MULTILINE) for matchNum, match in enumerate(matches, start=1): print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group())) for groupNum in range(0, len(match.groups())): groupNum = groupNum + 1 print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum))) # Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

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 Python, please visit: https://docs.python.org/3/library/re.html