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
  • Match everything enclosed
    (?:...)
  • Capture everything enclosed
    (...)
  • 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"
"
gimx

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""" ^ # get the title of this movie or series (?P<title> [-\w'"]+ # match separator to later replace into correct title (?P<separator> [ .] ) # note this *must* be lazy for the engine to work ltr not rtl (?: [-\w'"]+\2 )*? ) # start of movie vs serie check (?: # if this is an episode, lets match the season # number one way or another. if not, the year # of the movie (?: # series. can be a lot prettier if we used perl regex... # make sure this is not just a number in the title followed by our separator. # like, iron man 3 2013 or my.fictional.24.series (?! \d+ \2 ) # now try to match the season number (?: s (?: eason \2? )? )? (?P<season> \d\d? ) # needed to validate the last token is a dot, or whatever. (?: e\d\d? (?:-e?\d\d?)? | x\d\d? )? | # this is likely a movie, match the year (?P<year> [(\]]?\d{4}[)\]]? ) ) # make sure this ends with the separator, otherwise we # might be in the middle of something like "1080p" (?=\2) | # if we get here, this is likely still a movie. # match until one of the keywords (?= BOXSET | XVID | DIVX | LIMITED | UNRATED | PROPER | DTS | AC3 | AAC | BLU[ -]?RAY | HD(?:TV|DVD) | (?:DVD|B[DR]|WEB)RIP | \d+p | [hx]\.?264 ) ) """ test_str = ("the.black.adder.3x01.dish.and.dishonesty.dvdrip.xvid-dvdiso.avi\n" "the.black.adder.3x02.ink.and.incapability.dvdrip.xvid-dvdiso.avi\n" "the.black.adder.3x03.nob.and.nobility.dvdrip.xvid-dvdiso.avi\n" "the.black.adder.3x04.sense.and.senility.dvdrip.xvid-dvdiso.avi\n" "the.black.adder.3x05.amy.and.amiability.dvdrip.xvid-dvdiso.avi\n" "the.black.adder.3x06.duel.and.duality.dvdrip.xvid-dvdiso.avi\n" "Game.of.Thrones.S04E08.720p.HDTV.x264-KILLERS\n" "Futurama.S06.720p.BluRay.X264-REWARD-SiNNERS\n" "The.Grand.Budapest.Hotel.2014.1080p.BluRay.x264-BLOW\n" "Another.Earth.2011.LIMITED.720p.BluRay.X264-AMIABLE\n" "Game.of.Thrones.S04E09.720p.HDTV.x264-KILLERS\n" "The.Green.Mile.1999.720p.BluRay.x264-HDCLASSiCS\n" "Cosmos.A.Space.Time.Odyssey.S01E12.720p.HDTV.X264-DIMENSION\n" "Game.of.Thrones.S04E10.720p.HDTV.x264-KILLERS\n" "The Road 2009 Bluray DTS 720 p - TRiNiTY\n" "Dead Poets Society (1989) 720p Blu-ray x264 DTS MSubs [DDR]\n" "Garden.State.2004.720p.BluRay.X264-AMIABLE\n" "The.Royal.Tenenbaums.2001.1080p.BluRay.X264-AMIABLE\n" "Donnie.Darko.2001.1080p.PROPER.BluRay.x264-RRH\n" "Hot.Fuzz.720p.HDDVD.x264-SEPTiC\n" "True.Detective.S01E01.720p.HDTV.x264-KILLERS\n" "Eternal Sunshine of the Spotless Mind 2004 DiVERSiTY\n" "Non-Stop.2014.720p.BluRay.X264-AMIABLE\n" "True.Detective.S01E02.720p.HDTV.x264-KILLERS\n" "Fargo.S01E07.720p.HDTV.X264-DIMENSION\n" "Silicon.Valley.S01E08.720p.HDTV.x264-KILLERS\n" "True.Detective.S01E03.720p.HDTV.x264-KILLERS\n" "Fargo.S01E08.720p.HDTV.X264-DIMENSION\n" "Suits.S04E01.720p.HDTV.X264-DIMENSION\n" "True.Detective.S01E04.720p.HDTV.x264-KILLERS\n" "Fargo.S01E10.720p.HDTV.X264-DIMENSION\n" "Suits.S04E02.720p.HDTV.X264-DIMENSION\n" "True.Detective.S01E06.720p.HDTV.x264-2HD\n" "The.Descendants.2011.720p.BluRay.x264-ALLiANCE\n" "True.Detective.S01E07.720p.HDTV.x264-KILLERS\n" "Killing Season 2013 DVDRip x264-NoRBiT\n" "Game of Thrones S04E10 720p HDTV x264-KILLERS\n" "Game of Thrones S04E09 720p HDTV x264-KILLERS\n" "Game of Thrones S04E08 720p HDTV x264-KILLERS\n" "Game of Thrones S04E07 720p HDTV x264-KILLERS\n" "Game of Thrones S04E05 720p HDTV x264-KILLERS\n" "Game of Thrones S04E03 720p HDTV x264-KILLERS\n" "Game of Thrones S04E04 720p HDTV x264-KILLERS\n" "Game of Thrones S04E01 720p HDTV x264-KILLERS\n" "Game of Thrones S04E02 720p HDTV x264-2HD\n" "Game of Thrones S04E06 720p HDTV x264-DIMENSION\n" "Game of Thrones S04E10 HDTV x264-KILLERS\n" "Watch Dogs-RELOADED\n" "True Blood S07E01 720p HDTV x264-KILLERS\n" "Non-Stop 2014 720p BluRay X264-AMIABLE\n" "The Big Bang Theory S07E24 720p HDTV X264-DIMENSION\n" "Game of Thrones S04E09 HDTV x264-KILLERS\n" "24 S09E09 720p HDTV X264-DIMENSION\n" "The Big Bang Theory S07E23 720p HDTV X264-DIMENSION\n" "Game of Thrones S04E08 HDTV x264-KILLERS\n" "Suits S04E02 720p HDTV X264-DIMENSION\n" "Game of Thrones S04E06 INTERNAL 720p HDTV x264-BATV\n" "The Big Bang Theory S07E22 720p HDTV X264-DIMENSION\n" "Suits S04E01 720p HDTV X264-DIMENSION\n" "Watch Dogs Hotfix-RELOADED\n" "Wolfenstein The New Order-RELOADED\n" "The Big Bang Theory S07E20 720p HDTV X264-DIMENSION\n" "The Big Bang Theory S07E21 720p HDTV X264-DIMENSION\n" "Suits S04E03 720p HDTV X264-DIMENSION\n" "Californication S07E10 720p HDTV x264-DAA\n" "True Blood S07E01 HDTV x264-KILLERS\n" "24 S09E08 720p HDTV X264-DIMENSION\n" "Game of Thrones S04E07 HDTV x264-KILLERS\n" "Game of Thrones S04E05 HDTV x264-KILLERS\n" "Silicon Valley S01E01 720p HDTV x264-KILLERS\n" "Game of Thrones S04E06 HDTV x264-KILLERS\n" "24 S09E07 720p HDTV X264-DIMENSION\n" "Californication S07E08 720p HDTV x264-DAA\n" "The Big Bang Theory S07E19 720p HDTV X264-DIMENSION\n" "24 S09E06 720p HDTV X264-DIMENSION\n" "Californication S07E11 720p HDTV x264-KILLERS\n" "The Wolf of Wall Street 2013 DVDScr x264-HaM\n" "Game of Thrones S04E01 HDTV x264-KILLERS\n" "Game of Thrones S04E10 1080p HDTV x264-BATV\n" "The Wolf of Wall Street 2013 720p BluRay X264-AMIABLE\n" "Game of Thrones S04E02 HDTV x264-2HD\n" "Game of Thrones S04E03 HDTV x264-KILLERS\n" "The Big Bang Theory S07E24 HDTV x264-LOL\n" "24 S09E05 720p HDTV X264-DIMENSION\n" "Southpark Stick Of Truth-RELOADED\n" "300 Rise Of An Empire 2014 720p BluRay x264-BLOW\n" "Californication S07E09 INTERNAL 720p HDTV x264-BATV\n" "The Monuments Men 2014 720p BluRay x264-BLOW\n" "The Big Bang Theory S07E18 720p HDTV X264-DIMENSION\n" "Suits S04E01 HDTV x264-LOL\n" "The Wolf Of Wall Street 2013 720p BRRip x264 AAC-ViSiON\n" "Her 2013 720p BluRay x264-SPARKS\n" "The Big Bang Theory S07E17 720p HDTV X264-DIMENSION\n" "The Big Bang Theory S07E23 HDTV x264-LOL\n" "Family Guy S12E20 720p HDTV X264-DIMENSION\n" "The Walking Dead S04E15 720p HDTV x264-2HD\n" "300 Rise Of An Empire 2014 1080p BluRay x264-BLOW\n" "24 S09E04 720p HDTV X264-DIMENSION\n" "The Big Bang Theory S07E16 720p HDTV X264-DIMENSION\n" "Californication S07E07 720p HDTV x264-DAA\n" "Suits S04E02 HDTV x264-LOL\n" "Snowpiercer 2013 BRRip XviD AC3-AQOS\n" "Fargo S01E09 720p HDTV X264-DIMENSION\n" "Fargo S01E10 720p HDTV X264-DIMENSION\n" "Silicon Valley S01E08 720p HDTV x264-KILLERS\n" "The Walking Dead S04E12 720p HDTV x264-REMARKABLE\n" "Falling Skies S04E01 720p HDTV x264-KILLERS\n" "The Big Bang Theory S07E22 HDTV x264-LOL\n" "The Hobbit The Desolation of Smaug 2013 1080p BluRay x264-SPARKS\n" "Thor The Dark World 2013 720p HDRip XviD-AQOS\n" "Silicon Valley S01E02 720p HDTV x264-2HD\n" "Californication S07E06 720p HDTV x264-DAA\n" "True Detective S01E01 720p HDTV x264-KILLERS\n" "The Walking Dead S04E13 720p HDTV x264-2HD\n" "Robocop 2014 720p BluRay x264-SPARKS\n" "The Walking Dead S04E16 PROPER 720p HDTV x264-KILLERS\n" "The Big Bang Theory S07E21 HDTV x264-LOL\n" "The Grand Budapest Hotel 2014 720p BluRay x264-BLOW\n" "24 S09E03 720p HDTV X264-DIMENSION\n" "True Detective S01E03 720p HDTV x264-KILLERS\n" "Game of Thrones S04E04 REPACK HDTV x264-KILLERS\n" "24 S09E01 720p HDTV X264-DIMENSION\n" "24 S09E02 720p HDTV X264-DIMENSION\n" "Silicon Valley S01E03 720p HDTV x264-KILLERS\n" "The Big Bang Theory S07E15 720p HDTV X264-DIMENSION\n" "Dallas Buyers Club 2013 720p BRRip x264 AAC-ViSiON\n" "The Walking Dead S04E11 720p HDTV x264-2HD\n" "How I Met Your Mother S09E23-E24 720p HDTV x264-REMARKABLE\n" "Suits S03E16 720p HDTV x264-KILLERS\n" "GRID Autosport-RELOADED\n" "Silicon Valley S01E05 720p HDTV x264-KILLERS\n" "Vikings S02E10 720p HDTV x264-2HD\n" "Silicon Valley S01E06 720p HDTV x264-2HD\n" "300 Rise Of An Empire 2014 720p BRRiP XViD AC3-LEGi0N\n" "Californication S07E05 720p HDTV x264-2HD\n" "Silicon Valley S01E04 720p HDTV x264-KILLERS\n" "The Wolf Of Wall Street 2013 720p BRRip x264 AAC-ViSiON\n" "Snowpiercer 2013 BRRip XviD AC3-AQOS\n" "Dallas Buyers Club 2013 720p BRRip x264 AAC-ViSiON\n" "300 Rise Of An Empire 2014 720p BRRiP XViD AC3-LEGi0N\n" "American Hustle 2013 720p BRRip x264 AAC-ViSiON\n" "The Grand Budapest Hotel 2014 720p BRRiP XViD AC3-LEGi0N\n" "This Is The End 2013 720p BRRip x264 AAC-ViSiON\n" "Non-Stop 2014 720p BRRiP XViD AC3-LEGi0N\n" "A Million Ways To Die In The West 2014 720p New Webrip x264 AC3 TiTAN\n" "Captain Phillips 2013 720p BRRip x264 AAC-ViSiON\n" "The Lego Movie 2014 720p HDRip XviD-AQOS\n" "Now You See Me 2013 EXTENDED 720p BDRip x264 AAC-ViSiON\n" "Bad Neighbours 2014 720p WEBRIP x264 AC3-EVE\n" "Gravity 2013 720p WEBRiP XViD AC3-LEGi0N\n" "The Raid 2 2014 720p BRRiP XViD AC3-LEGi0N\n" "Under The Skin 2013 720p HDRIP x264 AC3 TiTAN\n" "The Hobbit The Desolation Of Smaug 2013 720p BRRip x264 AAC-ViSiON\n" "The Wolverine 2013 EXTENDED 720p BRRip x264 AAC-ViSiON\n" "Pompeii 2014 720p BRRiP XViD AC3-LEGi0N\n" "12 Years A Slave 2013 720p BDRip XviD AC3-ViSiON\n" "Frozen 2013 720p BRRip x264 AAC-ViSiON\n" "Man Of Steel 2013 720p BRRip x264 AAC-ViSiON\n" "Elysium 2013 720p WEBRiP XViD AC3-LEGi0N\n" "The Monuments Men 2014 720p BRRiP XViD AC3-LEGi0N\n" "Snowpiercer 2013 720p BDRip x264 AAC-ViSiON\n" "The Hunger Games Catching Fire 2013 IMAX EDITION 720p BRRip XviD AC3-ViSiON\n" "Robocop 2014 720p BRRiP XViD AC3-LEGi0N\n" "Jack Ryan Shadow Recruit 2014 720p BRRiP XViD AC3-LEGi0N\n" "Bad Words 2013 720p BRRiP XViD AC3-LEGi0N\n" "The Double 2014 720p HDRip x264 AAC-FooKaS\n" "Prisoners 2013 720p WEB-DL x264 AAC-ViSiON\n" "The Secret Life Of Walter Mitty 2013 720p BRRip x264 AAC-ViSiON\n" "Riddick 2013 UNRATED DC 720p WEB-DL XviD AC3-ELiTE\n" "47 Ronin 2013 720p WEB-DL XviD AC3-ELiTE\n" "Enders Game 2013 720p BRRiP XViD AC3-LEGi0N\n" "Oblivion 2013 720p BRRip x264 AAC-ViSiON\n" "That Awkward Moment 2014 720p BRRiP XViD AC3-LEGi0N\n" "The Raid 2 2014 BDRip x264-GECKOS\n" "47 Ronin 2013 720p BRRip x264 AAC-ViSiON\n" "The Grand Budapest Hotel 2014 720p WEBRIP x264 AC3-EVE\n" "Her 2013 720p BRRiP XViD AC3-LEGi0N\n" "Don Jon 2013 720p BRRip x264 AAC-ViSiON\n" "3 Days to Kill 2014 EXTENDED 720p BRRiP XViD AC3-LEGi0N\n" "The Lego Movie PROPER 720p HDRIP x264 AC3 TiTAN\n" "Thor The Dark World 2013 720p WEBRiP XViD AC3-LEGi0N\n" "White House Down 2013 720p BRRip x264 AAC-ViSiON\n" "The Hunger Games Catching Fire 2013 IMAX EDITION 720p BRRip x264 AAC-ViSiON\n" "The Lego Movie 2014 720p BRRiP XViD AC3-LEGi0N\n" "Bad Grandpa 2013 UNRATED 720p WEB-DL XviD AC3-ELiTE\n" "Non-Stop 2014 BRRip XviD AC3-SANTi\n" "Escape Plan 2013 720p BRRip x264 AAC-ViSiON\n" "Despicable Me 2 2013 720p BRRip x264 AAC-ViSiON\n" "Delivery Man 2013 720p BRRiP XViD AC3-LEGi0N\n" "Blended 2014 720p WEBRIP x264 AC3-EVE\n" "RoboCop 2014 720p WEBRIP x264 AC3-EVE\n" "The Great Gatsby 2013 720p BRRip x264 AAC-ViSiON\n" "Fast And Furious 6 2013 720p WEBRiP XViD AC3-LEGi0N\n" "A Million Ways to Die in the West 2014 HC 720p WEBRIP x264 AC3-MiLLENiUM\n" "Red 2 2013 720p BRRip x264 AAC-ViSiON\n" "The Lone Ranger 2013 720p BDRip x264 AAC-ViSiON\n" "World War Z 2013 UNRATED 720p BRRiP XViD AC3-LEGi0N\n" "Enemy 2013 LIMITED 720p BRRiP XViD AC3-LEGi0N\n" "Vampire Academy 2014 720p BRRiP XViD AC3-LEGi0N\n" "The Legend Of Hercules 2014 720p BRRip x264 AAC-ViSiON\n" "Non-Stop 2014 BDRip X264-AMIABLE\n" "Afflicted 2013 LIMITED 720p BRRiP XViD AC3-LEGi0N\n" "Street Fighter Assassin's Fist 2014 720p HDRip x264 AC3 TiTAN\n" "Veronica Mars 2014 HDRip AC3 XviD-EVO\n" "2 Guns 2013 720p BRRip x264 AAC-ViSiON\n" "Anchorman 2 The Legend Continues 2013 UNRATED 720p BRRip x264 AAC-ViSiON\n" "Lone Survivor 2013 720p WEBRiP XVID AC3-MAJESTIC\n" "Kick-Ass 2 2013 720p BRRip x264 AAC-ViSiON\n" "Jack Ryan Shadow Recruit 2014 HDRip XviD AC3-EVO\n" "Rush 2013 720p BRRip x264 AAC-ViSiON (Happy New Year from ViSiON!)\n" "Calvary 2014 720p BRRiP XViD AC3-LEGi0N\n" "The Hunger Games Catching Fire 2013 BRRip XviD AC3-SANTi\n" "Jack Ryan Shadow Recruit 2014 720p WEBRIP x264 AC3-EVE\n" "Thor The Dark World 2013 720p BRRip x264 AAC-ViSiON\n" "Bad Neighbours 2014 WEBRip NEW SOURCE Xvid Mp3 MiLLENiUM\n" "Prisoners 2013 720p BRRiP x264 AC3-LEGi0N\n" "Elysium 2013 720p BRRip x264 AAC-ViSiON\n" "Pacific Rim 2013 720p BRRip x264 AAC-ViSiON\n" "Saving Mr Banks 2013 720p BRRip x264 AAC-ViSiON\n" "The Monuments Men 2014 BDRip x264-COCAIN\n" "Joe 2013 1080p WEBRIP x264 AC3-EVE\n" "I Frankenstein 2014 720p BRRiP XViD AC3-LEGi0N\n" "The Secret Life Of Walter Mitty 2013 720p WEB-DL XviD AC3-ELiTE\n" "The Monuments Men 2014 BRRip XviD AC3-AQOS\n" "About Time 2013 720p BDRip XviD AC3-ELiTE\n" "Frozen 2013 720p BRRiP XViD AC3-LEGi0N\n" "Enemy 2013 720p WEBRiP XViD AC3-LEGi0N\n" "I Frankenstein 2014 BRRip XviD AC3-SANTi\n" "Out Of The Furnace 2013 720p BRRip x264 AAC-ViSiON\n" "Walk of Shame 2014 LIMITED 720p BRRiP XViD AC3-LEGi0N\n" "Iron Man 3 2013 720p BDRip x264 AAC-ViSiON\n" "The Monuments Men 2014 720p BDRIP x264 AC3-EVE\n" "Anchorman 2 The Legend Continues 2013 720p HDRIP x264 AC3 TiTAN\n" "Captain Phillips 2013 720p BRRiP XViD AC3-LEGi0N\n" "Jack Ryan Shadow Recruit 2014 BRRip XviD AC3-SANTi\n" "Cloudy With A Chance Of Meatballs 2 2013 720p BRRip x264 AAC-ViSiON\n" "my.fictional.24.series.s01e02.720p-firas ") matches = re.finditer(regex, test_str, re.IGNORECASE | 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