# 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