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

import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main(String[] args) { final String regex = "((?<==\\h)\\([A-Z][A-Z0-9_]+\\b(?=[<:\\;])|\\b(?<!<)[A-Z][A-Z0-9_]+(?=[^>\\)]))\\b|\\b((?<!=\\s)(?<!\\.)[A-Z]+\\d*?(?![\\(\\;:])(?=[,\\)>\\s]))\\b"; final String string = "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" + "}"; final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE); final Matcher matcher = pattern.matcher(string); while (matcher.find()) { System.out.println("Full match: " + matcher.group(0)); for (int i = 1; i <= matcher.groupCount(); i++) { System.out.println("Group " + i + ": " + matcher.group(i)); } } } }

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 Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html