Regular Expressions 101

Save & Share

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

@
@
xJm

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 = "#OxJm)\n" + " (?(DEFINE)\n" + " # Tokens that act as one unit.\n" + " (?<rem>\n" + " /\\* (?:[^*]++|\\*(?!/))*+ \\*/ # block comment\n" + " | //[^\\r\\n]++(?:\\r?+\\n?+|$) # line comment\n" + " )\n" + " (?<squote> '(?:[^'\\\\]++|\\\\.)*+')\n" + " (?<dquote> \"(?:[^\"\\\\]++|\\\\.)*+\")\n" + " (?<rquote>R \"(?<delim>[^(]*+)\\((?:[^)]++|\\)(?!\\k\\{delim\\}\"))*+\\)\\k\\{delim\\}\")\n" + " (?<string_literal>\n" + " (?<string_literal_helper>(?&squote)|(?&dquote)|(?&rquote))\n" + " (?:\\s*+(?&string_literal_helper))*+\n" + " )\n" + " (?<int_literal>[-+]?+\\s*+\n" + " (?:\n" + " 0[xX](?<hex>[a-fA-F\\d]++ (?:'(?&hex))?+)\n" + " | 0[bB](?<bin> [01]++ (?:'(?&bin))?+)\n" + " | 0 (?<oct> [0-7]++ (?:'(?&oct))?+)\n" + " | (?<dec> \\d++ (?:'(?&dec))?+)\n" + " )\n" + " (?:[uU](?:ll|LL|[lLzZ])?+)?+\n" + " )\n" + " (?<float_literal>[-+]?+\\s*+\n" + " (?:\n" + " 0[xX] (?&hex)\n" + " (?: \\.(?&hex) (?<matched>))?+\n" + " (?:[pP][-=]?+(?&hex) (?<matched>))?+\n" + " (?(matched)|(?!)) # at least one of the above must match\n" + " | (?&dec)\n" + " (?: \\.(?&dec) (?<matched>))?+\n" + " (?:[eE][-=]?+(?&dec) (?<matched>))?+\n" + " (?(matched)|(?!)) # at least one of the above must match\n" + " )\n" + " (?:f16|f32|f64|f128|bf16|F16|F32|F64|F128|BF16|[flFL])?+\n" + " )\n" + " (?<literal> (?&string_literal) | (?&float_literal) | (?&int_literal) )\n\n" + " (?<token>(?&literal) | (?&rem))\n" + " (?<params> \\((?:(?&token)|[^()] | (?&params))*+\\))\n" + " (?<tparams> <(?:(?&token)|[^<>()]|(?&tparams) |(?&params))*+> )\n" + " (?<brace_list> \\{(?:(?&token)|[^\\{\\}()]|(?&brace_list)|(?&params))*+\\})\n" + " (?<index> \\[(?:(?&token)|[^[\\]] |(?&index) )*+\\])\n" + " (?<lambda_capture> (?&index)) # not the same but this will still match it correctly\n\n" + " # a simple symbol\n" + " (?<symbol>[a-zA-Z_][a-zA-Z\\d_]*+)\n" + " # symbols used for operator overloading\n" + " (?<operator_symbol>\n" + " operator\\s*+\n" + " (?:\n" + " &&|\\|\\||\\[\\]|\\(\\)|\\+\\+|--|\"\"|<=>|->\\*?+|,|(?:new|delete)(?:\\s*+(?:\\[\\]))?+\n" + " | (?:<<|>>|[-+~!%^&*<=>])=?+\n" + " | (?&type)\n" + " )\n" + " )\n\n" + " (?<scoped_type_symbol_name>\n" + " (?:::\\s*+)?+\n" + " (?<scoped_type_symbol_name_helper>\n" + " (?:template(?<template_keyword_matched>)\\s++)?+\n" + " (?&symbol)\\s*+\n" + " # Check for template parameters\n" + " (?(template_keyword_matched)\n" + " # MANDITORY if template keyword is used.\n" + " (?:(?&tparams)\\s*+)\n" + " | # OPTIONAL if template keyword is omitted.\n" + " # TODO: Look into possible edge cases where this might\n" + " # not work as expected.\n" + " (?:(?&tparams)\\s*+)?+ \n" + " )\n" + " (?:\\s*+::\\s*+(?&scoped_type_symbol_name_helper))?+\n" + " )\n" + " )\n\n" + " (?<scoped_value_symbol_name>\n" + " (?:::\\s*+)?+\n" + " (?<scoped_value_symbol_name_helper>\n" + " (?:template(?<template_keyword_matched>)\\s++)?+\n" + " (?:(?&operator_symbol)(?<operator_symbol_matched>)|(?&symbol))\\s*+\n" + " # Check for template parameters\n" + " (?(template_keyword_matched)\n" + " # MANDITORY if template keyword is used.\n" + " (?:(?&tparams)\\s*+)\n" + " | # OPTIONAL if template keyword is omitted.\n" + " # TODO: Look into possible edge cases where this might\n" + " # not work as expected.\n" + " (?:(?&tparams)\\s*+)?+ \n" + " )\n" + " (?(operator_symbol_matched)\n" + " # Operator symbol is at deepest scope.\n" + " | # If didn't match operator symbol, then see if there is a deeper scope\n" + " (?:\\s*+::\\s*+(?&scoped_value_symbol_name_helper))?+\n" + " )\n" + " )\n" + " )\n\n" + " # types\n" + " (?<cv>\n" + " const \\b(?:\\s++volitile\\b)?+\n" + " | volitile\\b(?:\\s++const \\b)?+\n" + " )\n" + " (?<user_type>\n" + " (?:typename\\s*+)?+\n" + " (?&scoped_type_symbol_name)\n" + " )\n\n" + " (?<type>\n" + " # not going to do function pointers yet\n" + " (?:(?&cv)\\s++)?+\n" + " (?:auto\\b\n" + " # Yes, this will capture illegal types such as short char, but I'm ok with that.\n" + " # This is to help document, not validate the code!\n" + " | (?: (?:(?:un)?+signed) \\b (?<matched>))?+\n" + " (?:\\s*+ (?:short|long) \\b (?<matched>))?+\n" + " (?:\\s*+ (?:long) \\b (?<matched>))?+\n" + " (?:\\s*+ (?:int|char) \\b (?<matched>))?+\n" + " (?(matched)|(?!)) # at least one of the items above must have matched\n" + " | ((?:long\\s++)?+double|float)\\b\n" + " | decltype\\s*+(?&params)\n" + " | (?&user_type)\n" + " )\n" + " (?:\\s*+(?&cv))?+\n" + " # pointers\n" + " (?:\\s*+\\*\\s*+(?&cv)?+)*+\n" + " # reference\n" + " &?+\n" + " )\n\n" + " # expressions\n" + " (?<fn_called_or_var>\n" + " (?&scoped_value_symbol_name)\\s*+\n" + " (?&params)?+\n" + " )\n" + " (?<lambda>(?&lambda_capture)\\s*+(?&params)\\s*+(?&brace_list))\n" + " (?<evaluable>\n" + " (?:\\*\\s*+)*+ # dereference\n" + " (?:(?&fn_called_or_var)|(?&lambda)\\s*+(?&params)|(?&params)|(?&literal))\n" + " (?:\\s*+(?&index))*+\n" + " )\n" + " (?<binary_op> &&|<=>|<<|>>|\\|\\||[-+*/%|&<=>])\n" + " (?<unary_prefix_op> --|\\+\\+|[-+!~])\n" + " (?<unary_suffix_op> --|\\+\\+)\n\n" + " (?<expression>\n" + " (?:(?&unary_prefix_op)++\\s*+)?+(?&evaluable)(\\s*+(?&unary_suffix_op)++)?+\n" + " (?:\\s*+(?&binary_op)\\s*+(?&expression))*+\n" + " )\n" + " )\n" + " (?<whitespace>^[ \\t]*+)\n" + " (?:\n" + " # check for enum\n" + " enum\\s++(?<enum_name>(?&symbol))\n" + " | # check for template\n" + " (?:template\\s*+(?<template_parameters>(?&tparams)))?+\\s*+\n" + " (?:\n" + " # check for struct/class\n" + " (?:struct|class)\\s++(?<class_name>(?&symbol))\n" + " | # check for function or variable\n" + " (?<extern>extern\\s++)?+\n" + " (?<static>static\\s++)?+\n" + " (?<constexpr>constexpr\\s++)?+\n" + " (?<static>static\\s++)?+\n" + " (?<return_type>auto(?<auto_return_type>)|(?&type))\\s*+\n" + " (?<symbol_name>(?&scoped_value_symbol_name))\\s*+\n" + " (?<function_parameters>(?&params)\n" + " (?(auto_return_type)\n" + " # optional trailing return type\n" + " (?:\\s*+->\\s*+(?<return_type>(?&type)))?+\n" + " )\n" + " )?+\n" + " )\n" + " )\n"; final String string = "\n" + " template <typename ElementGenerator, typename StoredType, typename...Rest>\n" + " constexpr auto generate_tuple(\n" + " ElementGenerator&& element_generator, DiagnosticReportCode code, bool database_query, StoredType\n" + " , Rest&&...args\n" + " ) {\n" + " static_assert(!std::is_same<StoredType, DiagnosticReportCode>::value\n" + " "; final Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS | Pattern.MULTILINE); final Matcher matcher = pattern.matcher(string); if (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