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
No Match

r"
"
g

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"(above|below|title|data[0-9]*|header[0-9]*|caption|above|label[0-9]*) *= +([^{}|\n]+)[^| \n]" test_str = ("{{Infobox\n" " |name = {{subst:PAGENAME}}\n" " |bodystyle = \n\n" " |titlestyle = \n" " |abovestyle = background:#cfc;\n" " |subheaderstyle =\n" " |title = Test Infobox\n" " |above = Above text\n" " |subheader = Subheader above image\n" " |subheader2 = Second subheader\n\n" " |imagestyle = \n" " |captionstyle = \n" " | image = [[File:example.png|200px|alt=Example alt text]]\n" " |caption = Caption displayed below example.png\n\n" " |headerstyle = background:#ccf;\n" " |labelstyle = background:#ddf;\n" " |datastyle = \n\n" " |header1 = Header defined alone\n" " | label1 = \n" " | data1 = \n" " |header2 = \n" " | label2 = Label defined alone does not display (needs data, or is suppressed)\n" " | data2 = \n" " |header3 =\n" " | label3 = \n" " | data3 = Data defined alone\n" " |header4 = All three defined (header, label, data, all with same number)\n" " | label4 = does not display (same number as a header)\n" " | data4 = does not display (same number as a header)\n" " |header5 =\n" " | label5 = Label and data defined (label)\n" " | data5 = Label and data defined (data)\n\n" " |belowstyle = background:#ddf;\n" " |below = Below text\n" "}}\n\n" "{{Infobox\n" " | title = Top level title\n" " | header1 = {{Infobox | decat = yes | child = yes\n" " | title = First subsection\n" " | label1= Label 1.1\n" " | data1 = Data 1.1\n" " }}\n" " | header2 = {{Infobox | decat = yes | child = yes\n" " | title = Second subsection\n" " | label1= Label 2.1\n" " | data1 = Data 2.1\n" " }}\n" " | belowstyle = \n" " | below = Below text\n" "}}\n\n" "{{Infobox\n" " |name = {{subst:PAGENAME}}\n" " |bodystyle = width:20em\n\n" " |titlestyle = \n" " |title = Test Infobox\n\n" " |headerstyle = \n" " |labelstyle = width:33%\n" " |datastyle = \n\n" " |header1 = \n" " | label1 = Label 1\n" " | data1 = Data 1\n" " |header2 = \n" " | label2 = Label 2\n" " | data2 = Data 2\n" " |header3 = \n" " | label3 = Label 3\n" " | data3 = Data 3\n" " |header4 = Header 4\n" " | label4 = \n" " | data4 = \n" " |header5 = \n" " | label5 = Label 5\n" " | data5 = Data 5: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n\n" " |belowstyle = \n" " |below = Below text\n" "}}\n\n\n" "{{Infobox\n" " |name = {{subst:PAGENAME}}\n" " |bodystyle = width:20em\n\n" " |titlestyle = \n" " |title = Test Infobox\n\n" " |headerstyle = \n" " |labelstyle = width:33%\n" " |datastyle = \n\n" " |header1 = \n" " | label1 = Label 1\n" " | data1 = Data 1\n" " |header2 = \n" " | label2 = Label 2\n" " | data2 = Data 2\n" " |header3 = \n" " | label3 = Label 3\n" " | data3 = Data 3\n" " |header4 = Header 4\n" " | label4 = \n" " | data4 = \n" " |header5 = \n" " | label5 = Label 5\n" " | data5 = Data 5: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n\n" " |belowstyle = \n" " |below = Below text\n" "}}\n\n\n" "{{Infobox\n" " |name = {{subst:PAGENAME}}\n" " |bodystyle = width:20em\n\n" " |titlestyle = \n" " |title = Test Infobox\n\n" " |headerstyle = \n" " |labelstyle = width:33%\n" " |datastyle = \n\n" " |header1 = \n" " | label1 = Label 1\n" " | data1 = Data 1\n" " |header2 = \n" " | label2 = Label 2\n" " | data2 = Data 2\n" " |header3 = \n" " | label3 = Label 3\n" " | data3 = Data 3\n" " |header4 = Header 4\n" " | label4 = \n" " | data4 = \n" " |header5 = \n" " | label5 = Label 5\n" " | data5 = Data 5: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n\n" " |belowstyle = \n" " |below = Below text\n" "}}\n\n\n" "{{Infobox\n" " |name = {{subst:PAGENAME}}\n" " |bodystyle = width:20em\n\n" " |titlestyle = \n" " |title = Test Infobox\n\n" " |headerstyle = \n" " |labelstyle = width:33%\n" " |datastyle = \n\n" " |header1 = \n" " | label1 = Label 1\n" " | data1 = Data 1\n" " |header2 = \n" " | label2 = Label 2\n" " | data2 = Data 2\n" " |header3 = \n" " | label3 = Label 3\n" " | data3 = Data 3\n" " |header4 = Header 4\n" " | label4 = \n" " | data4 = \n" " |header5 = \n" " | label5 = Label 5\n" " | data5 = Data 5: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n\n" " |belowstyle = \n" " |below = Below text\n" "}}\n\n\n" "{{Infobox\n" " | headerstyle = background-color:#eee;\n" " | labelstyle = background-color:#eee;\n" " | header1 = Main 1\n" " | header2 = Main 2\n" " | data3 = {{Infobox | subbox = yes\n" " | headerstyle = background-color:#ccc;\n" " | labelstyle = background-color:#ddd;\n" " | header1 = Sub 3-1\n" " | header2 = Sub 3-2\n" " | label3 = Label 3-3 | data3 = Data 3-3\n" " }}\n" " | data4 = {{Infobox | subbox = yes\n" " | labelstyle = background-color:#ccc;\n" " | label1 = Label 4-1 | data1 = Data 4-1\n" " }}\n" " | label5 = Label 5 | data5 = Data 5\n" " | header6 = Main 6\n" "}}\n\n\n" "{{Infobox\n" " | headerstyle = background-color:#eee;\n" " | labelstyle = background-color:#eee;\n" " | header1 = Main 1\n" " | header2 = Main 2\n" " | data3 = {{Infobox | subbox = yes\n" " | headerstyle = background-color:#ccc;\n" " | labelstyle = background-color:#ddd;\n" " | header1 = Sub 3-1\n" " | header2 = Sub 3-2\n" " | label3 = Label 3-3 | data3 = Data 3-3\n" " }}\n" " | data4 = {{Infobox | subbox = yes\n" " | labelstyle = background-color:#ccc;\n" " | label1 = Label 4-1 | data1 = Data 4-1\n" " }}\n" " | label5 = Label 5 | data5 = Data 5\n" " | header6 = Main 6\n" "}}\n\n\n\n") matches = re.finditer(regex, test_str) 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