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

/
/
gmx

Test String

Code Generator

Generated Code

# coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility import re regex = r""" ^(?:(?P<name>[\p{L}\.\ ]+)\n)? (?:(?P<co>[\p{L}\.\ \/]+)\n)? (?P<street>[\p{L}\ \p{N}’\.]+)\n (?:(?<city>[\p{L}\ ]+)\n)? (?P<postal_code>[\p{N}\ ]+)\ (?P<postal_city>[\p{L}\ ]+)(?:\((?<province>[\p{L}\ ]+)\))?\n (?P<country>[\p{L}\ ]+)? $ """ test_str = ("Herr Franz Huber\n" "Beethovenstrasse 12\n" "1010 WIEN\n" "AUSTRIA\n\n\n" "Mr Willy Janssens\n" "Lange Stationsstraat 352\n" "3000 LEUVEN\n" "BELGIUM\n\n\n" "M. Emile Dubois\n" "Rue du Diamant 215\n" "4800 VERVIERS\n" "BELGIUM\n\n\n" "Mr Thor Nielsen\n" "Tietgensgade 137\n" "8800 VIBORG\n" "DENMARK\n\n\n" "Mr Torben Raldorf\n" "PO Box 100\n" "COPENHAGEN\n" "1004 VIBORG\n" "DENMARK\n\n\n" "Peter Mogensen\n" "c/o Fictional Company\n" "Niels Bohrs Alle 23\n" "Dalum\n" "5230 Odense M\n" "Danmark\n\n\n" "Mr Asko Teirila\n" "PO Box 511\n" "39140 AKDENMAA\n" "FINLAND\n\n\n" "M. Robert MARIN\n" "Rue de l’Eglise\n" "Dunes\n" "82340 AUVILLAR\n" "FRANCE\n\n\n" "Mme Marie PAGE\n" "23 Rue de Grenell\n" "75700 PARIS CEDEX\n" "FRANCE\n\n\n" "Mrs F Meier\n" "Weberstr. 2\n" "53113 BONNN\n" "GERMANY\n\n\n" "Mr P Kunde\n" "Lange Str. 12\n" "04103 LEIPZIG\n" "GERMANY\n\n\n" "Mr George Latsis\n" "Alkamenou 37\n" "117 80 ATHENS\n" "GREECE\n\n\n" "Mr Jon Jonsson\n" "Einimel 80\n" "107 REYKJAVIK\n" "ICELAND\n\n\n" "Sig. Giovanni Masci\n" "via Garibaldi 27\n" "47037 RIMINI RN\n" "ITALY\n\n\n" "M. Jaques Muller\n" "71 route de Longway\n" "4750 PETANGE\n" "LUXEMBOURG\n\n\n" "Mr J van Dieten\n" "Morsstr 111\n" "2312 BK LEIDEN\n" "THE NETHERLANDS\n" "(There should be a double space between the postcode and the post town)\n\n\n" "Herr Hans Hansen\n" "Svingen 22\n" "9230 BEKKEHAUG\n" "NORWAY\n\n\n" "Senhor Carlos Manuel Pereira\n" "Av das A’Augsa Livres\n" "Monte Trigo\n" "7220 PORTEL\n" "PORTUGAL\n\n\n" "Rosalina Silva\n" "R Conde Redondo 80\n" "1192 LISBOA CODEX\n" "PORTUGAL\n\n\n" "Sra Ana Jimenez\n" "Mimbreras 4A\n" "03201 ELCHE (Alicante)\n" "SPAIN\n" "(The province should be included in brackets after the town)\n\n\n" "Fru Inger Lilja\n" "Vasavagen 3 4tr\n" "582 20 LINKOPING\n" "SWEDEN\n\n\n" "M. Andre Perret\n" "Schanzenstrasse 7\n" "3030 BERNE\n" "SWITZERLAND") matches = re.finditer(regex, test_str, re.MULTILINE | re.VERBOSE) for matchNum, match in enumerate(matches, start=1): print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group())) for groupNum in range(0, len(match.groups())): groupNum = groupNum + 1 print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum))) # Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

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 Python, please visit: https://docs.python.org/3/library/re.html