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