Regular Expressions 101

Save & Share

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

/
/
xmg

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 = "(?(DEFINE)\n" + " (?# Periodic elements )\n" + " (?<Hydrogen>H)\n" + " (?<Helium>He)\n" + " (?<Lithium>Li)\n" + " (?<Beryllium>Be)\n" + " (?<Boron>B)\n" + " (?<Carbon>C)\n" + " (?<Nitrogen>N)\n" + " (?<Oxygen>O)\n" + " (?<Fluorine>F)\n" + " (?<Neon>Ne)\n" + " (?<Sodium>Na)\n" + " (?<Magnesium>Mg)\n" + " (?<Aluminum>Al)\n" + " (?<Silicon>Si)\n" + " (?<Phosphorus>P)\n" + " (?<Sulfur>S)\n" + " (?<Chlorine>Cl)\n" + " (?<Argon>Ar)\n" + " (?<Potassium>K)\n" + " (?<Calcium>Ca)\n" + " (?<Scandium>Sc)\n" + " (?<Titanium>Ti)\n" + " (?<Vanadium>V)\n" + " (?<Chromium>Cr)\n" + " (?<Manganese>Mn)\n" + " (?<Iron>Fe)\n" + " (?<Cobalt>Co)\n" + " (?<Nickel>Ni)\n" + " (?<Copper>Cu)\n" + " (?<Zinc>Zn)\n" + " (?<Gallium>Ga)\n" + " (?<Germanium>Ge)\n" + " (?<Arsenic>As)\n" + " (?<Selenium>Se)\n" + " (?<Bromine>Br)\n" + " (?<Krypton>Kr)\n" + " (?<Rubidium>Rb)\n" + " (?<Strontium>Sr)\n" + " (?<Yttrium>Y)\n" + " (?<Zirconium>Zr)\n" + " (?<Niobium>Nb)\n" + " (?<Molybdenum>Mo)\n" + " (?<Technetium>Tc)\n" + " (?<Ruthenium>Ru)\n" + " (?<Rhodium>Rh)\n" + " (?<Palladium>Pd)\n" + " (?<Silver>Ag)\n" + " (?<Cadmium>Cd)\n" + " (?<Indium>In)\n" + " (?<Tin>Sn)\n" + " (?<Antimony>Sb)\n" + " (?<Tellurium>Te)\n" + " (?<Iodine>I)\n" + " (?<Xenon>Xe)\n" + " (?<Cesium>Cs)\n" + " (?<Barium>Ba)\n" + " (?<Lanthanum>La)\n" + " (?<Cerium>Ce)\n" + " (?<Praseodymium>Pr)\n" + " (?<Neodymium>Nd)\n" + " (?<Promethium>Pm)\n" + " (?<Samarium>Sm)\n" + " (?<Europium>Eu)\n" + " (?<Gadolinium>Gd)\n" + " (?<Terbium>Tb)\n" + " (?<Dysprosium>Dy)\n" + " (?<Holmium>Ho)\n" + " (?<Erbium>Er)\n" + " (?<Thulium>Tm)\n" + " (?<Ytterbium>Yb)\n" + " (?<Lutetium>Lu)\n" + " (?<Hafnium>Hf)\n" + " (?<Tantalum>Ta)\n" + " (?<Tungsten>W)\n" + " (?<Rhenium>Re)\n" + " (?<Osmium>Os)\n" + " (?<Iridium>Ir)\n" + " (?<Platinum>Pt)\n" + " (?<Gold>Au)\n" + " (?<Mercury>Hg)\n" + " (?<Thallium>Tl)\n" + " (?<Lead>Pb)\n" + " (?<Bismuth>Bi)\n" + " (?<Polonium>Po)\n" + " (?<Astatine>At)\n" + " (?<Radon>Rn)\n" + " (?<Francium>Fr)\n" + " (?<Radium>Ra)\n" + " (?<Actinium>Ac)\n" + " (?<Thorium>Th)\n" + " (?<Protactinium>Pa)\n" + " (?<Uranium>U)\n" + " (?<Neptunium>Np)\n" + " (?<Plutonium>Pu)\n" + " (?<Americium>Am)\n" + " (?<Curium>Cm)\n" + " (?<Berkelium>Bk)\n" + " (?<Californium>Cf)\n" + " (?<Einsteinium>Es)\n" + " (?<Fermium>Fm)\n" + " (?<Mendelevium>Md)\n" + " (?<Nobelium>No)\n" + " (?<Lawrencium>Lr)\n" + " (?<Rutherfordium>Rf)\n" + " (?<Dubnium>Db)\n" + " (?<Seaborgium>Sg)\n" + " (?<Bohrium>Bh)\n" + " (?<Hassium>Hs)\n" + " (?<Meitnerium>Mt)\n" + " (?<Darmstadtium>Ds)\n" + " (?<Roentgenium>Rg)\n" + " (?<Copernicium>Cn)\n" + " (?<Nihonium>Nh)\n" + " (?<Flerovium>Fl)\n" + " (?<Moscovium>Mc)\n" + " (?<Livermorium>Lv)\n" + " (?<Tennessine>Ts)\n" + " (?<Oganesson>Og)\n" + " (?# Regex )\n" + " (?<Element>(?&Actinium)|(?&Silver)|(?&Aluminum)|(?&Americium)|(?&Argon)|(?&Arsenic)|(?&Astatine)|(?&Gold)|(?&Barium)|(?&Beryllium)|(?&Bohrium)|(?&Bismuth)|(?&Berkelium)|(?&Bromine)|(?&Boron)|(?&Calcium)|(?&Cadmium)|(?&Cerium)|(?&Californium)|(?&Chlorine)|(?&Curium)|(?&Copernicium)|(?&Cobalt)|(?&Chromium)|(?&Cesium)|(?&Copper)|(?&Carbon)|(?&Dubnium)|(?&Darmstadtium)|(?&Dysprosium)|(?&Erbium)|(?&Einsteinium)|(?&Europium)|(?&Iron)|(?&Flerovium)|(?&Fermium)|(?&Francium)|(?&Fluorine)|(?&Gallium)|(?&Gadolinium)|(?&Germanium)|(?&Helium)|(?&Hafnium)|(?&Mercury)|(?&Holmium)|(?&Hassium)|(?&Hydrogen)|(?&Indium)|(?&Iridium)|(?&Iodine)|(?&Krypton)|(?&Potassium)|(?&Lanthanum)|(?&Lithium)|(?&Lawrencium)|(?&Lutetium)|(?&Livermorium)|(?&Moscovium)|(?&Mendelevium)|(?&Magnesium)|(?&Manganese)|(?&Molybdenum)|(?&Meitnerium)|(?&Sodium)|(?&Niobium)|(?&Neodymium)|(?&Neon)|(?&Nihonium)|(?&Nickel)|(?&Nobelium)|(?&Neptunium)|(?&Nitrogen)|(?&Oganesson)|(?&Osmium)|(?&Oxygen)|(?&Protactinium)|(?&Lead)|(?&Palladium)|(?&Promethium)|(?&Polonium)|(?&Praseodymium)|(?&Platinum)|(?&Plutonium)|(?&Phosphorus)|(?&Radium)|(?&Rubidium)|(?&Rhenium)|(?&Rutherfordium)|(?&Roentgenium)|(?&Rhodium)|(?&Radon)|(?&Ruthenium)|(?&Antimony)|(?&Scandium)|(?&Selenium)|(?&Seaborgium)|(?&Silicon)|(?&Samarium)|(?&Tin)|(?&Strontium)|(?&Sulfur)|(?&Tantalum)|(?&Terbium)|(?&Technetium)|(?&Tellurium)|(?&Thorium)|(?&Titanium)|(?&Thallium)|(?&Thulium)|(?&Tennessine)|(?&Uranium)|(?&Vanadium)|(?&Tungsten)|(?&Xenon)|(?&Ytterbium)|(?&Yttrium)|(?&Zirconium)|(?&Zinc))\n" + " (?<Num>(?:[1-9]\\d*)?)\n" + " (?<ElementGroup>(?:(?&Element)(?&Num))+)\n" + " (?<ElementParenthesesGroup>\\((?&ElementGroup)+\\)(?&Num))\n" + " (?<ElementSquareBracketGroup>\\[(?:(?:(?&ElementParenthesesGroup)(?:(?&ElementGroup)|(?&ElementParenthesesGroup))+)|(?:(?:(?&ElementGroup)|(?&ElementParenthesesGroup))+(?&ElementParenthesesGroup)))\\](?&Num))\n" + ")\n" + "^((?<Brackets>(?&ElementSquareBracketGroup))|(?<Parentheses>(?&ElementParenthesesGroup))|(?<Group>(?&ElementGroup)))+$"; final String string = "-- These entries are valid --\n" + "C\n" + "CH\n" + "CH3\n" + "O2\n" + "C2CH2H2\n" + "(CH)\n" + "(CH3)\n" + "(CH3NO4)\n" + "(CH3NO4)2\n" + "[CH3(NO4)]\n" + "[(NO4)CH]2\n" + "[(NO4)(CH3)]\n" + "(CH3)2CFCOO(CH2)2Si[NO3(CH3)2]2\n\n" + "-- These entries are invalid --\n" + "N0\n" + "N01\n" + "A\n" + "c\n" + "(CH[NO4])\n" + "[(NO4)]\n" + "[NO4]\n" + "[NO4(CH])\n" + "(CH3)2CFCOO(CH2)2Si[NO3(CH3)2]2Cl[N2]"; final Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS | 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