[kwlug-disc] Fwd: Regular Expression to Match Movie Titles and Year and Ignore the rest.

John Driezen jdriezen at sympatico.ca
Wed Jan 1 00:42:19 EST 2025


On 2024-12-31 11:36 p.m., Ronald Barnes via kwlug-disc wrote:
> John Driezen wrote on 2024-12-31 15:29:
>
>> This program gives the following error messages when run.
>
> Which version of Python?
>
Python 3.12.3

> 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
>
Thank you.  I will try that code.



More information about the kwlug-disc mailing list