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

/
/
gi

Test String

Substitution

Processing...

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 = "(<mark[^>]*>.*?<\\/mark>)|((&nbsp;|.)((?:#|#)(?:[a-z0-9_\\u00c0-\\u00d6\\u00d8-\\u00f6\\u00f8-\\u00ff\\u0100-\\u024f\\u0253-\\u0254\\u0256-\\u0257\\u{0259}\\u\\{025b\\}\\u{0263}\\u{0268}\\u\\{026f\\}\\u{0272}\\u{0289}\\u\\{028b\\}\\u\\{02bb\\}\\u0300-\\u036f\\u1e00-\\u1eff\\u0400-\\u04ff\\u0500-\\u0527\\u2de0-\\u2dff\\ua640-\\ua69f\\u0591-\\u05bf\\u05c1-\\u05c2\\u05c4-\\u05c5\\u\\{05c7\\}\\u05d0-\\u05ea\\u05f0-\\u05f4\\ufb12-\\ufb28\\ufb2a-\\ufb36\\ufb38-\\ufb3c\\u\\{fb3e\\}\\ufb40-\\ufb41\\ufb43-\\ufb44\\ufb46-\\ufb4f\\u0610-\\u061a\\u0620-\\u065f\\u066e-\\u06d3\\u06d5-\\u06dc\\u06de-\\u06e8\\u06ea-\\u06ef\\u06fa-\\u06fc\\u\\{06ff\\}\\u0750-\\u077f\\u\\{08a0\\}\\u08a2-\\u08ac\\u08e4-\\u08fe\\ufb50-\\ufbb1\\ufbd3-\\ufd3d\\ufd50-\\ufd8f\\ufd92-\\ufdc7\\ufdf0-\\ufdfb\\ufe70-\\ufe74\\ufe76-\\ufefc\\u200c-\\u200c\\u0e01-\\u0e3a\\u0e40-\\u0e4e\\u1100-\\u11ff\\u3130-\\u3185\\ua960-\\ua97f\\uac00-\\ud7af\\ud7b0-\\ud7ff\\uffa1-\\uffdc\\u30a1-\\u30fa\\u30fc-\\u30fe\\uff66-\\uff9f\\uff10-\\uff19\\uff21-\\uff3a\\uff41-\\uff5a\\u3041-\\u3096\\u3099-\\u309e\\u3400-\\u4dbf\\u4e00-\\u9fff\\u{3003}\\u{3005}\\u\\{303b\\}]*[a-z_\\u00c0-\\u00d6\\u00d8-\\u00f6\\u00f8-\\u00ff\\u0100-\\u024f\\u0253-\\u0254\\u0256-\\u0257\\u{0259}\\u\\{025b\\}\\u{0263}\\u{0268}\\u\\{026f\\}\\u{0272}\\u{0289}\\u\\{028b\\}\\u\\{02bb\\}\\u0300-\\u036f\\u1e00-\\u1eff\\u0400-\\u04ff\\u0500-\\u0527\\u2de0-\\u2dff\\ua640-\\ua69f\\u0591-\\u05bf\\u05c1-\\u05c2\\u05c4-\\u05c5\\u\\{05c7\\}\\u05d0-\\u05ea\\u05f0-\\u05f4\\ufb12-\\ufb28\\ufb2a-\\ufb36\\ufb38-\\ufb3c\\u\\{fb3e\\}\\ufb40-\\ufb41\\ufb43-\\ufb44\\ufb46-\\ufb4f\\u0610-\\u061a\\u0620-\\u065f\\u066e-\\u06d3\\u06d5-\\u06dc\\u06de-\\u06e8\\u06ea-\\u06ef\\u06fa-\\u06fc\\u\\{06ff\\}\\u0750-\\u077f\\u\\{08a0\\}\\u08a2-\\u08ac\\u08e4-\\u08fe\\ufb50-\\ufbb1\\ufbd3-\\ufd3d\\ufd50-\\ufd8f\\ufd92-\\ufdc7\\ufdf0-\\ufdfb\\ufe70-\\ufe74\\ufe76-\\ufefc\\u200c-\\u200c\\u0e01-\\u0e3a\\u0e40-\\u0e4e\\u1100-\\u11ff\\u3130-\\u3185\\ua960-\\ua97f\\uac00-\\ud7af\\ud7b0-\\ud7ff\\uffa1-\\uffdc\\u30a1-\\u30fa\\u30fc-\\u30fe\\uff66-\\uff9f\\uff10-\\uff19\\uff21-\\uff3a\\uff41-\\uff5a\\u3041-\\u3096\\u3099-\\u309e\\u3400-\\u4dbf\\u4e00-\\u9fff\\u{3003}\\u{3005}\\u\\{303b\\}][a-z0-9_\\u00c0-\\u00d6\\u00d8-\\u00f6\\u00f8-\\u00ff\\u0100-\\u024f\\u0253-\\u0254\\u0256-\\u0257\\u{0259}\\u\\{025b\\}\\u{0263}\\u{0268}\\u\\{026f\\}\\u{0272}\\u{0289}\\u\\{028b\\}\\u\\{02bb\\}\\u0300-\\u036f\\u1e00-\\u1eff\\u0400-\\u04ff\\u0500-\\u0527\\u2de0-\\u2dff\\ua640-\\ua69f\\u0591-\\u05bf\\u05c1-\\u05c2\\u05c4-\\u05c5\\u\\{05c7\\}\\u05d0-\\u05ea\\u05f0-\\u05f4\\ufb12-\\ufb28\\ufb2a-\\ufb36\\ufb38-\\ufb3c\\u\\{fb3e\\}\\ufb40-\\ufb41\\ufb43-\\ufb44\\ufb46-\\ufb4f\\u0610-\\u061a\\u0620-\\u065f\\u066e-\\u06d3\\u06d5-\\u06dc\\u06de-\\u06e8\\u06ea-\\u06ef\\u06fa-\\u06fc\\u\\{06ff\\}\\u0750-\\u077f\\u\\{08a0\\}\\u08a2-\\u08ac\\u08e4-\\u08fe\\ufb50-\\ufbb1\\ufbd3-\\ufd3d\\ufd50-\\ufd8f\\ufd92-\\ufdc7\\ufdf0-\\ufdfb\\ufe70-\\ufe74\\ufe76-\\ufefc\\u200c-\\u200c\\u0e01-\\u0e3a\\u0e40-\\u0e4e\\u1100-\\u11ff\\u3130-\\u3185\\ua960-\\ua97f\\uac00-\\ud7af\\ud7b0-\\ud7ff\\uffa1-\\uffdc\\u30a1-\\u30fa\\u30fc-\\u30fe\\uff66-\\uff9f\\uff10-\\uff19\\uff21-\\uff3a\\uff41-\\uff5a\\u3041-\\u3096\\u3099-\\u309e\\u3400-\\u4dbf\\u4e00-\\u9fff\\u{3003}\\u{3005}\\u\\{303b\\}]*)))((&nbsp;|\\W)|$)"; final String string = "<p>laboris nisi ut aliquip ex ea commodo</p><p>&nbsp;#adf -sdaf</p><p>dsf w#home</p>\n\n" + "<p>Lorem <mark contenteditable=\"false\">#ipsum</mark> dolor sit amet, </p><p>consectetur adipisicing elit, sed do </p><p>eiusmod</p><p>tempor incididunt ut <mark contenteditable=\"false\">#labore</mark> et </p><p>#dolore </p><p>magna aliqua. Ut enim ad minim </p><p>veniam,</p><p>quis @nostrud exercitation ul <mark contenteditable=\"false\">#lamco</mark> </p>#else"; final String subst = "$1$3<mark contenteditable=\\\"false\\\">$4</mark>$5"; final Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); final Matcher matcher = pattern.matcher(string); // The substituted value will be contained in the result variable final String result = matcher.replaceAll(subst); System.out.println("Substitution result: " + 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 Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html