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

/
/
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"(.*Request.*\[(?P<req_ip>.*)\:.*\].*GZIP)\n(.*)\n(.*)\n(.*Play.*\'(?P<watched>.*)\'.*)\n(.*User is\s(?P<who>.*)\s\(.*)\n(.*Device is(?P<device>.*)\.)\n(.*)\n(.*Completed.*\=(?P<movie_time>\d+)\&k.*\&time\=(?P<played_time>\d+)\s\(.*)" test_str = ("[2016-07-03 13:45:04,777] INFO PlayQueueInputStream - brent listening to \"Firehouse/Love Of A Lifetime.MP3\"\n" "[2016-07-02 21:21:10,615] INFO TranscodeInputStream - Starting transcoder: [/var/subsonic/transcode/ffmpeg] [-i] [/pub/media/mp3/Rock/TedNugent/Cat Scratch Fever/01 Cat Scratch Fever.wma] [-map] [0:0] [-b:a] [192k] [-v] [0] [-f] [mp3] [-] \n" "[2016-07-02 19:46:57,695] INFO TranscodeInputStream - Starting transcoder: [/var/subsonic/transcode/ffmpeg] [-i] [/pub/media/mp3/Rock/Kansas/01 Carry on Wayward Son.wma] [-map] [0:0] [-b:a] [192k] [-v] [0] [-f] [mp3] [-]\n" "[2016-07-02 19:28:31,635] INFO TranscodeInputStream - Starting transcoder: [/var/subsonic/transcode/ffmpeg] [-i] [/pub/media/mp3/Rock/Queen/Greatest Hits [Parlophone]/14 Flash.wma] [-map] [0:0] [-b:a] [192k] [-v] [0] [-f] [mp3] [-] \n" "[2016-07-02 22:12:39,647] INFO TranscodeInputStream - Starting transcoder: [/var/subsonic/transcode/ffmpeg] [-i] [/pub/media/mp3/Pop/Katy Perry/One of the Boys/02 I Kissed a Girl.m4a] [-ab] [192k] [-ar] [44100] [-ac] [2] [-v] [0] [-f] [mp3] [-]\n" "[2016-06-26 11:10:29,662] INFO TranscodeInputStream - Starting transcoder: [/var/subsonic/transcode/ffmpeg] [-i] [/pub/media/mp3/Christian/The Almost/2013 - Fear Inside Our Bones/03 - I'm Down.flac] [-ab] [192k] [-ar] [44100] [-ac] [2] [-v] [0] [-f] [mp3] [-] \n" "[2016-07-04 19:47:41,874] INFO DownloadController - Downloading 'This is not a test (2015)/15. Like A Match (Garcia's Remix).mp3' to Player 103 [caryn]\n" "INSERT INTO MEDIA_FILE VALUES(146130,'/pub/media/mp3/Alternative/WorldParty/Goodbye Jumbo/06 And I Fell Back Alone.flac','/pub/media/mp3/Alternative','MUSIC','flac','06 And I Fell Back Alone','Goodbye Jumbo','WorldParty','WorldParty',NULL,NULL,NULL,NULL,NULL,FALSE,NULL,20943444,NULL,NULL,NULL,'/pub/media/mp3/Alternative/WorldParty/Goodbye Jumbo',0,NULL,NULL,'2013-05-24 16:42:12.000000000','2013-05-24 16:42:12.000000000','2016-07-07 20:02:08.217000000','1969-12-31 18:00:00.000000000',TRUE,4)\n\n" "/*C2445758*/SET SCHEMA PUBLIC\n" "CONNECT USER SA\n" "DISCONNECT\n\n" "Jul 06, 2016 23:40:04 [0x7fdbf063b700] DEBUG - Loading Movie 'Batman v Superman: Dawn of Justice' XML from /var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Metadata/Movies/2/2d037fa20a5177c5442ca5d6fc44c7aea0940d6.bundle/Contents/_combined/\n" "Jul 08, 2016 02:03:55 [0x7fdbf063b700] DEBUG - Loading Artist Liquido from XML file: /var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Metadata/Artists/f/e4d9aa73581b1e97d433a111542ca12b5a97f29.bundle/Contents/_combined/Info.xml\n\n" "Adding xxxxxxxxxxxxxxxxxxxx as token for 42 sections for jefferyblake\n\n" "Jul 03, 2016 18:51:03 [0x7fdc0b125700] DEBUG - Request: [172.56.6.129:54338] GET /:/timeline?duration=6886122&key=/library/metadata/67850&playQueueItemID=2926&ratingKey=67850&state=playing&time=5286338 (13 live) TLS GZIP\n" "Jul 03, 2016 18:51:03 [0x7fdc0b125700] DEBUG - Auth: We found auth token (xxxxxxxxxxxxxxxxxxxx), enabling token-based authentication.\n" "Jul 03, 2016 18:51:03 [0x7fdc0b125700] DEBUG - Client [0B3645F6-539D-4428-86DB-E954F15992F9] reporting timeline state playing, progress of 5286338/6886122ms for guid=, ratingKey=67850 url=, key=/library/metadata/67850, containerKey=, metadataId=67850\n" "Jul 03, 2016 18:51:03 [0x7fdc0b125700] DEBUG - Play progress on 67850 'Pitch Perfect 2' - got played 5286338 ms by account 4033586!\n" "Jul 03, 2016 18:51:03 [0x7fdc0b125700] DEBUG - [Now] User is deliaswaz (ID: 4033586)\n" "Jul 03, 2016 18:51:03 [0x7fdc0b125700] DEBUG - [Now] Device is iOS (Alan's iPhone).\n" "Jul 03, 2016 18:51:03 [0x7fdc0b125700] DEBUG - [Now] Updated play state for /library/metadata/67850.\n" "Jul 03, 2016 18:51:03 [0x7fdc0fbff700] DEBUG - Completed: [172.56.6.129:54338] GET /:/timeline?duration=6886122&key=/library/metadata/67850&playQueueItemID=2926&ratingKey=67850&state=playing&time=5286338 (13 live) TLS GZIP 6ms 203 bytes 200 (pipelined: 5)") 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