Regular Expressions 101

Save & Share

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

r"
"
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""" ^ # Start of string (?: # A container group: (\+49|0049|\+\(49\)|\(\+49\))? [ ()\/-]* # German: country code (?(1)|0)1(?:5[12579]|6[023489]|7[0-9]) # trunk prefix and company code | # or (\+43|0043|\+\(43\)|\(\+43\))? [ ()\/-]* # Austrian: country code (?(2)|0)6(?:64|(?:50|6[0457]|7[0678]|8[0168]|9[09])) # trunk prefix and company code ) [ ()\/-]* # zero or more spaces, parens, / and - \d(?:[ \/-]*\d){6,7} # a digit and then six or seven occurrences of space, / or - and a digit \s* # zero or more whites $ # end of string """ test_str = ("################################### This is allowed ##########################\n" "#### Longes German format ### https://en.wikipedia.org/wiki/Telephone_numbers_in_Germany\n" "017698600189\n" "0176 98600189\n" "0176 98 600 189\n" "+4917698600189\n" "+49 17698600189\n" "0049 17698600189\n" "004917698600189\n" "(+49)17698600189\n" "(+49) 17698600189\n" "+(49)17698600189\n" "+(49) 17698600189\n" "+(49) 176 98600189\n" "+(49) 176 98 600 189\n" "+49 176 98 600 189\n" "+49 (176) 98 600 189\n\n" "#### Shortest German format ### https://en.wikipedia.org/wiki/Telephone_numbers_in_Germany\n" "01609860018\n" "0160 9860018\n" "0160 98 600 18\n" "+491609860018\n" "+49 1609860018\n" "0049 1609860018\n" "00491609860018\n" "(+49)1609860018\n" "(+49) 1609860018\n" "+(49)1609860018\n" "+(49) 1609860018\n" "+(49) 160 9860018\n" "+(49) 160 98 600 18\n" "+49 160 98 600 18\n" "+49 (160) 98 600 18\n\n" "#### Different Prefix https://en.wikipedia.org/wiki/Telephone_numbers_in_Germany\n" "017698600189\n" "01609860018\n" "01519860018\n" "01709860018\n" "01719860018\n" "01759860018\n" "01529860018\n" "01799860018\n" "01789860018\n" "01779860018\n" "01559860018\n" "01629860018\n" "01729860018\n" "01739860018\n" "01749860018\n\n" "### Austrian numbers in different possible formats: https://www.tarife.at/ratgeber/vorwahl-handynetze\n" "06641234567\n" "066412345678\n\n" "+436641234567\n" "+4366412345678\n\n" "0664 1234567\n" "0664 12345678\n\n" "0664 123 456 7\n" "0664 123 456 78\n\n" "0664 123 4567\n" "0664 123 45678\n\n" "+43 664 1234567\n" "+43 664 12345678\n\n" "+43 6641234567\n" "+43 66412345678\n\n" "+43 664 123 4567\n" "+43 664 123 45678\n\n" "+43 664 123 456 7\n" "+43 664 123 456 78\n\n" "(+43) 664 123 456 7\n" "(+43) 664 123 456 78\n\n" "+(43) 664 123 456 7\n" "+(43) 664 123 456 78\n\n" "### Austrian numbers with same format but different prefix\n" "06801234567\n" "068012345678\n\n" "06881234567\n" "068812345678\n\n" "06811234567\n" "068112345678\n\n" "06991234567\n" "069912345678\n\n" "06641234567\n" "066412345678\n\n" "06811234567\n" "068112345678\n\n" "06671234567\n" "066712345678\n\n" "06501234567\n" "065012345678\n\n" "06761234567\n" "067612345678\n\n" "06501234567\n" "065012345678\n\n" "06781234567\n" "067812345678\n\n" "06771234567\n" "067712345678\n\n" "06901234567\n" "069012345678\n\n" "06651234567\n" "066512345678\n\n" "06861234567\n" "068612345678\n\n" "############################################################################################\n" "############################################################################################\n" "################################### This is NOT valid ######################################\n" "012345678901234\n" "123w345345345345\n" "0123456789101191919\n\n" "### Too short\n" "0160986001\n" "016098600\n" "01609860\n" "0160986\n" "016098\n" "01609\n" "0160\n" "016\n" "01\n" "0\n\n" "### Too short with +49 (same is true with +43)\n" "+490160986001\n" "+49016098600\n" "+4901609860\n" "+490160986\n" "+49016098\n" "+4901609\n" "+490160\n" "+49016\n" "+4901\n" "+490\n\n" "### Too short with 0049 (same is true with 0043)\n" "00490160986001\n" "0049016098600\n" "004901609860\n" "00490160986\n" "0049016098\n" "004901609\n" "00490160\n" "0049016\n" "004901\n" "00490\n\n\n" "### Too long for Germany\n" "0176123456789\n" "01761234567891\n" "017612345678912\n" "0176123456789123\n" "01761234567891234\n" "017612345678912345\n" "0176123456789123456\n\n" "+49176123456789\n" "+491761234567891\n" "+4917612345678912\n" "+49176123456789123\n" "+491761234567891234\n" "+4917612345678912345\n" "+49176123456789123456\n\n" "0049176123456789\n" "00491761234567891\n" "004917612345678912\n" "0049176123456789123\n" "00491761234567891234\n" "004917612345678912345\n" "0049176123456789123456\n\n" "### Not German and not Austrain format for mobile numbers\n" "+12127319863\n" "+13322014056\n" "+12126712234\n" "+427532697710\n" "+417868150810\n" "+287533002875\n") 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