[kwlug-disc] Fwd: Regular Expression to Match Movie Titles and Year and Ignore the rest.
Ronald Barnes
ron at ronaldbarnes.ca
Tue Dec 31 23:36:19 EST 2024
John Driezen wrote on 2024-12-31 15:29:
> This program gives the following error messages when run.
Which version of Python?
I ran it through 3.10 and got nothing like that.
> Suggestions and improvements welcome.
Try this:
import os
import re
with os.scandir() as i:
for entry in i:
if entry.is_file():
## Use named groups for easy use of captured data:
## Also, put it all in one regex for easy positional grabbing of
## elements:
## Finally, wrapped for legibility in email, etc.:
info = re.search(
"^"
+ "(?P<title>.*)"
+ "\.(?P<year>\d{4})\."
+ "(?P<res>\d+p)"
+ ".*\.(?P<ext>.*)"
+ "$",
entry.name)
print( f"title: {info.group('title')}")
print( f"year: {info.group('year')}")
print( f"resolution: {info.group('res')}")
print( f"extension: {info.group('ext')}")
Output:
title: Zero.Dark.Thirty
year: 2012
resolution: 720p
extension: mp4
More information about the kwlug-disc
mailing list