Regular Expressions 101

Save & Manage Regex

  • Current Version: 1
  • Save & Share
  • Community Library

Flavor

  • PCRE2 (PHP)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java
  • .NET 7.0 (C#)
  • Rust
  • PCRE (Legacy)
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests
Sponsors
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
Processing...

Test String

Code Generator

Generated Code

import re regex = re.compile(r"^(Title|Release Year|Format|Stars): *(.*)", flags=re.MULTILINE) test_str = ("Title: Blazing Saddles\n" "Release Year: 1974\n" "Format: VHS\n" "Stars: Mel Brooks, Clevon Little, Harvey Korman, Gene Wilder, Slim Pickens, Madeline Kahn\n\n" "Title: Casablanca\n" "Release Year: 1942\n" "Format: DVD\n" "Stars: Humphrey Bogart, Ingrid Bergman, Claude Rains, Peter Lorre\n\n" "Title: Charade\n" "Release Year: 1953\n" "Format: DVD\n" "Stars: Audrey Hepburn, Cary Grant, Walter Matthau, James Coburn, George Kennedy\n\n" "Title: Cool Hand Luke\n" "Release Year: 1967\n" "Format: VHS\n" "Stars: Paul Newman, George Kennedy, Strother Martin\n\n" "Title: Butch Cassidy and the Sundance Kid\n" "Release Year: 1969\n" "Format: VHS\n" "Stars: Paul Newman, Robert Redford, Katherine Ross\n\n" "Title: The Sting\n" "Release Year: 1973\n" "Format: DVD\n" "Stars: Robert Redford, Paul Newman, Robert Shaw, Charles Durning\n\n" "Title: The Muppet Movie\n" "Release Year: 1979\n" "Format: DVD\n" "Stars: Jim Henson, Frank Oz, Dave Geolz, Mel Brooks, James Coburn, Charles Durning, Austin Pendleton\n\n" "Title: Get Shorty \n" "Release Year: 1995\n" "Format: DVD\n" "Stars: John Travolta, Danny DeVito, Renne Russo, Gene Hackman, Dennis Farina\n\n" "Title: My Cousin Vinny\n" "Release Year: 1992\n" "Format: DVD\n" "Stars: Joe Pesci, Marrisa Tomei, Fred Gwynne, Austin Pendleton, Lane Smith, Ralph Macchio\n\n" "Title: Gladiator\n" "Release Year: 2000\n" "Format: Blu-Ray\n" "Stars: Russell Crowe, Joaquin Phoenix, Connie Nielson\n\n" "Title: Star Wars\n" "Release Year: 1977\n" "Format: Blu-Ray\n" "Stars: Harrison Ford, Mark Hamill, Carrie Fisher, Alec Guinness, James Earl Jones\n\n" "Title: Raiders of the Lost Ark\n" "Release Year: 1981\n" "Format: DVD\n" "Stars: Harrison Ford, Karen Allen\n\n" "Title: Serenity\n" "Release Year: 2005\n" "Format: Blu-Ray\n" "Stars: Nathan Fillion, Alan Tudyk, Adam Baldwin, Ron Glass, Jewel Staite, Gina Torres, Morena Baccarin, Sean Maher, Summer Glau, Chiwetel Ejiofor\n\n" "Title: Hooisers\n" "Release Year: 1986\n" "Format: VHS\n" "Stars: Gene Hackman, Barbara Hershey, Dennis Hopper\n\n" "Title: WarGames\n" "Release Year: 1983\n" "Format: VHS\n" "Stars: Matthew Broderick, Ally Sheedy, Dabney Coleman, John Wood, Barry Corbin\n\n" "Title: Spaceballs\n" "Release Year: 1987\n" "Format: DVD\n" "Stars: Bill Pullman, John Candy, Mel Brooks, Rick Moranis, Daphne Zuniga, Joan Rivers\n\n" "Title: Young Frankenstein\n" "Release Year: 1974\n" "Format: VHS\n" "Stars: Gene Wilder, Kenneth Mars, Terri Garr, Gene Hackman, Peter Boyle\n\n" "Title: Real Genius\n" "Release Year: 1985\n" "Format: VHS\n" "Stars: Val Kilmer, Gabe Jarret, Michelle Meyrink, William Atherton\n\n" "Title: Top Gun\n" "Release Year: 1986\n" "Format: DVD\n" "Stars: Tom Cruise, Kelly McGillis, Val Kilmer, Anthony Edwards, Tom Skerritt\n\n" "Title: MASH\n" "Release Year: 1970 \n" "Format: DVD\n" "Stars: Donald Sutherland, Elliot Gould, Tom Skerritt, Sally Kellerman, Robert Duvall\n\n" "Title: The Russians Are Coming, The Russians Are Coming\n" "Release Year: 1966\n" "Format: VHS\n" "Stars: Carl Reiner, Eva Marie Saint, Alan Arkin, Brian Keith\n\n" "Title: Jaws\n" "Release Year: 1975 \n" "Format: DVD\n" "Stars: Roy Scheider, Robert Shaw, Richard Dreyfuss, Lorraine Gary \n\n" "Title: 2001: A Space Odyssey\n" "Release Year: 1968\n" "Format: DVD\n" "Stars: Keir Dullea, Gary Lockwood, William Sylvester, Douglas Rain\n\n" "Title: Harvey\n" "Release Year: 1950 \n" "Format: DVD\n" "Stars: James Stewart, Josephine Hull, Peggy Dow, Charles Drake\n\n" "Title: Knocked Up\n" "Release Year: 2007\n" "Format: Blu-Ray\n" "Stars: Seth Rogen, Katherine Heigl, Paul Rudd, Leslie Mann\n\n\n\n") matches = regex.finditer(test_str) for match_num, match in enumerate(matches, start=1): print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}") for group_num, group in enumerate(match.groups(), start=1): print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")

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