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

/
/
gm

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"^(?<pos>.{4})\s(?<naam>.{25})\s(?<plaats>.{25})\s(?<tijd>.{8})" test_str = (" 25 Gerry Trimpe Middelburg 1:46:04\n" " 26 Sylvia Glerum-witkam Delta Sport 1:46:35\n" " 27 Annette de bot THOR 1:47:27\n" " 28 Ernesta Verburg Dreischor 1:47:40\n" " 29 Daniella Evers Middelburg 1:47:48\n" " 30 Corine Poot Arnhem 1:48:05\n" " 31 Lian Zorge Zierikzee 1:48:37\n" " 32 Miranda Kwakkelaar Middelburg 1:50:42\n" " 33 Lianne wijsman Delta Sport 1:51:17\n" " 34 Esther Oldenburger Brielle 1:51:40\n" " 35 Katinka Schotanus Kloetinge 1:51:47\n" " 36 Heleen van der Linden PAC 1:52:04\n" " 37 Cora Verheij Abbenbroek 1:54:22\n" " 38 Connie Huibregtse Delta Sport 1:54:55\n" " 39 Jacqueline Meijers AV '56 1:55:07\n" " 40 Josien van Waarde Middelburg 1:55:52\n" " 41 Liesbet Peeters Kasterlee 1:56:02\n" " 42 Marianne zwart Helmond 1:56:41\n" " 43 MariƎlle Lauwers Helmond 1:56:43\n" " 44 Ieke de Jonge 's-Gravenhage 1:57:48\n" " 45 Joke van Paassen Rockanje 1:58:00\n" " 46 Mireille van der Veer Brielle 1:58:02\n" " 47 Sylvia Verkaart Kapelle 1:58:33\n" " 48 Lenneke Ruijsink AV '56 1:59:06\n" " 49 Nicole Ehbel Steenbergen Nb 1:59:16\n" " 50 Laila Heshof Steenbergen Nb 1:59:18\n" " 51 Marleen Verboom Delta Sport 1:59:20\n" " 52 Mandy Bootsma Strijen 2:00:16\n" " 53 Debora Hampel- van Kapel Oud-Vossemeer 2:01:24\n" " 54 Janneke van Westen Sint Philipsland 2:01:29\n" " 55 Petra van den Doel Zierikzee 2:02:41\n" " 56 Jolanda ter Steege Dinteloord 2:03:42\n" " 57 Marleen Hozee Vlissingen 2:03:52\n" " 58 Cynthia Janse Delta Sport 2:03:54\n" " 59 Esther Brouwer Vlissingen 2:04:46\n" " 60 Regine Boogert Middelburg 2:04:48\n" " 61 Miriam Bijl Zuid-Beijerland 2:04:51\n" " 62 Mariel Kers Dordrecht 2:18:43\n" " 63 Pippi Langkous Scharendijke 2:23:26\n" " 64 Monique Coppejans Nieuwdorp Zld 2:33:34") matches = re.finditer(regex, test_str, re.MULTILINE) 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