Regular Expressions 101

Save & Share

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

/
/
gixu

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 = "# Definição de ordens\n" + "(?(DEFINE)(?P<unidades> um|dois|tr[eê]s|quatro|cinco|meia|se(?:is|te)|oito|nove ))\n" + "(?(DEFINE)(?P<dezenas_dez> d(?:ez(?:e(?:nove|sse(?:is|te))|oito)?|oze)|onze|treze|catorze|quinze ))\n" + "(?(DEFINE)(?P<dezenas_exceto_dez> vinte|trinta|(?:quar|cinqu|se(?:ss|t)|oit|nov)enta ))\n" + "(?(DEFINE)(?P<dezenas> (?P>dezenas_dez)|(?P>dezenas_exceto_dez) ))\n" + "(?(DEFINE)(?P<dezenas_ou_unidades> (?P>dezenas)|(?P>unidades) ))\n" + "(?(DEFINE)(?P<centenas> ce(?:m|ntos?)|(?:qu(?:atroc|inh)|(?:du|tre|se(?:is|te)|oito|nove)[cz])entos? ))\n" + "(?(DEFINE)(?P<mil> mil ))\n" + "(?(DEFINE)(?P<milhoes> (?:(?:[mb]|tr)i|qu(?:atri|inqua))lh(?:[aã]o|[oõ]es) ))\n" + "(?(DEFINE)(?P<s> [ ]+ ))\n" + "(?(DEFINE)(?P<e> e(?P>s) ))\n\n\n" + "#Regras\n" + "\\b(?:\n\n" + " #1. Entre dezenas>=20 e unidades\n" + " (?P>dezenas_exceto_dez)(?P>s)(?=(?P>unidades)\\b)\n" + "|\n" + " #2. Entre centenas e dezenas ou unidades\n" + " (?P>centenas)(?P>s)(?=(?P>dezenas_ou_unidades)\\b)\n" + "|\n" + " #3-4. Entre milhares...\n" + " (?P>mil)(?P>s)(?=\n" + " (?:\n" + " #3. ... e dezenas ou unidades\n" + " (?P>dezenas_ou_unidades)\n" + " |\n" + " #4. ... e centenas, somente se não houver dezenas ou unidades\n" + " (?P>centenas)(?!(?P>s)(?P>e)?(?P>dezenas_ou_unidades)\\b)\n" + " )\n" + " \\b\n" + " )\n" + "|\n" + " #5. Entre milhões e dezenas ou unidades (que não sejam unidades ou dezenas de milhões ou unidades ou dezenas de milhares)\n" + " (?P>milhoes)(?P>s)(?=\n" + " (?P>dezenas_ou_unidades)(?!(?P>s)(?P>e)?(?:(?P>unidades)(?P>s)(?P>e)?)?(?:(?P>milhoes)|(?P>mil))\\b)\\b\n" + " \n" + " )\n\n" + ")\n" + "\\b\n\n" + "#Reset\n" + "\\K"; final String string = "vinte cinco\n" + "vinte cinco mil\n" + "vinte cinco reais\n" + "vinte cinco reais e vinte cinco centavos\n" + "aaa bbb ccc\n" + "dois mil trezentos\n" + "dois mil trezentos quarenta cinco\n" + "dois milhões trinta um\n" + "dois milhões trinta um mil\n" + "dois milhões trinta mil\n" + "cento vinte três milhões quatrocentos cinquenta seis mil setecentos oitenta nove"; final String subst = "e "; final Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.COMMENTS | Pattern.UNICODE_CASE); 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