Regular Expressions 101

Save & Share

  • Regex Version: ver. 13
  • 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"
"
gx

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""" # Parse table of SWPC event data (?P<event>\d{4}) \s+ (?P<multi_event_flag>\+)? \s+ (?P<time_start_flag>\w)? (?P<time_start>\d{4}) \s+ (?P<time_peak_flag>\w)? (?: (?P<time_peak>\d{4})| /{4} ) \s+ (?P<time_end_flag>\w)? (?P<time_end>\d{4}) \s+ (?P<observatory>\w{3}) \s+ (?P<quality>\w) \s+ (?P<type>\w{3}) \s+ (?: (?P<location> (?P<lat_hg>\w\d{2}) (?P<long_hg>\w\d{2}) )| (?P<frequency_range> (?P<from>\w+) \- (?P<to>\w+) )| (?P<frequency> (\d{3}) )| (?P<no_freq> (/{4}) ) ) \s+ (?P<particulars_1>[\w/\.\+]+) \s+ (?: (?P<particulars_2> (?:\d\.\d\w[\-\+]\d{2})? )| (?P<particulars_3>\w{3}?)| () ) \s+ (?: (?P<regnum>\d{4})| () ) \n """ test_str = (":Product: 20050316events.txt\n" ":Created: 2005 Mar 17 1802 UT\n" ":Date: 2005 03 16\n" "# Prepared by the U.S. Dept. of Commerce, NOAA, Space Environment Center.\n" "# Please send comments and suggestions to SEC.Webmaster@noaa.gov \n" "#\n" "# Missing data: ////\n" "# Updated every 30 minutes.\n" "# Edited Events for 2005 Mar 16\n" "#\n" "#Event Begin Max End Obs Q Type Loc/Frq Particulars Reg#\n" "#-------------------------------------------------------------------------------\n\n" "1590 0318 0318 0318 LEA G RBR 245 51 \n\n" "1620 + 0348 //// 1635 COM C RSP 30-80 CTM/1 \n\n" "1600 + 0408 0410 0412 LEA G RBR 245 210 \n\n" "1730 + 0422 1214 1511 COM G RNS 245 160 \n\n" "1610 0522 0528 0532 G10 5 XRA 1-8A B4.2 1.9E-04 \n\n" "1630 0741 0744 0748 G10 5 XRA 1-8A B1.9 7.3E-05 \n" "1630 0741 //// 0743 SVI C RSP 025-089 III/1 \n\n" "1640 1052 //// 1053 SVI U RSP 025-046 III/1 \n\n" "1650 1125 1125 1125 SVI G RBR 245 85 \n" "1650 1125 1125 1125 SVI G RBR 410 46 \n\n" "1660 1150 //// 1150 SVI C RSP 025-041 III/1 \n\n" "1670 + 1205 1208 1210 G12 5 XRA 1-8A B3.5 8.1E-05 0742\n" "1670 1206 1206 1209 SAG G RBR 245 100 0742\n" "1670 1207 1207 1207 SAG G RBR 410 56 0742\n" "1670 1209 1210 1210 G12 5 XFL S02W48 3.0E+02 6.5E+02 0742\n" "1670 1210 //// 2228 SAG C RSP 110-180 CTM/1 0742\n\n" "1680 + 1217 1221 1223 G12 5 XRA 1-8A B5.1 1.4E-04 0742\n" "1680 + 1219 1219 1219 SVI G RBR 410 310 0742\n" "1680 1221 1222 1223 G12 5 XFL S03W50 7.5E+02 2.3E+03 0742\n\n" "1690 + 1242 1245 1247 G12 5 XRA 1-8A B3.1 7.7E-05 0742\n" "1690 1246 1246 1247 G12 5 XFL S07W48 4.3E+02 9.1E+02 0742\n\n" "1700 + 1306 //// 1306 SAG C RSP 030-053 III/1 \n\n" "1710 + 1318 1322 1332 G12 5 XRA 1-8A B3.0 2.3E-04 0742\n" "1710 1322 1327 1331 G12 5 XFL S07W53 2.3E+02 4.6E+02 0742\n\n" "1720 + 1339 //// 1339 SVI C RSP 025-034 III/1 \n\n" "1750 1450 //// 1451 SVI C RSP 025-180 III/1 \n\n" "1760 + 1542 1547 1554 G12 5 XRA 1-8A B3.8 2.1E-04 0742\n" "1760 + 1544 1544 1546 SAG G RBR 245 380 0742\n" "1760 + 1544 //// 1545 SAG C RSP 030-180 III/1 0742\n" "1760 1546 1550 1553 G12 5 XFL S07W54 4.8E+02 1.0E+03 0742\n\n" "1770 1633 //// 1634 SAG C RSP 030-050 III/1 \n\n" "1780 1755 //// 1756 SAG C RSP 030-053 III/1 \n\n" "1790 + 1851 //// 1851 PAL C RSP 025-085 III/1 \n\n" "1800 + 1947 1953 1959 G12 5 XRA 1-8A B5.9 3.3E-04 0742\n" "1800 1952 1957 1957 G12 5 XFL S06W57 7.3E+02 1.8E+03 0742\n\n" "1810 + 2029 2034 2036 G12 5 XRA 1-8A B5.3 1.6E-04 0742\n" "1810 + 2030 //// 2032 CUL C RSP 20-350 III/2 0742\n" "1810 B2033 2033 2035 G12 5 XFL S07W54 7.8E+02 2.0E+03 0742\n" "1810 + 2035 2035 2035 PAL G RBR 245 110 0742\n" "1810 2035 U2035 2035 PAL G RBR 410 63 0742\n\n" "1820 + 2049 2053 2056 G12 5 XRA 1-8A B3.6 1.3E-04 0742\n" "1820 2051 //// 2051 CUL C RSP 40-160 III/1 0742\n" "1820 2053 2053 A2053 G12 5 XFL S02W55 1.8E+02 3.3E+02 0742\n\n" "1830 + 2116 2116 2116 PAL G RBR 245 630 \n" "1830 + 2116 2116 2116 PAL G RBR 410 130 \n" "1830 2116 2116 2116 PAL G RBR 606 67 \n" "1830 2116 //// 2116 CUL C RSP 40-460 III/2 \n\n" "1840 + 2304 2314 2316 G12 5 XRA 1-8A B9.8 4.6E-04 0742\n" "1840 2309 2313 2316 G12 5 XFL S07W56 2.0E+03 5.3E+03 0742\n" "1840 2313 2313 2318 HOL 3 FLA S06W52 SF ERU 0742\n" "1840 2313 //// 2315 PAL C RSP 025-180 V/1 0742\n" "1840 + 2314 2314 2314 PAL G RBR 245 160 0742\n" "1840 + 2314 2314 2314 PAL G RBR 410 1700 0742\n" "1840 + 2314 //// 2315 CUL C RSP 18-500 III/3 0742\n") matches = re.finditer(regex, test_str, 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