Gambit2007
Gambit2007

Reputation: 3958

How to check if a string matches a certain pattern?

I have a string which is basically a file path of an .mp4 file.

I want to test if the file path is matching one of the following patterns:

/*.mp4   (nothing before the slash, anything after)
*/*.mp4  (anything before and after the slash)
[!A]*.mp4  (anything before the extension, **except** for the character 'A')

What would be the best way to achieve this? Thanks!

EDIT:

I'm not looking to test if the file ends with .mp4, i'm looking to test if it ends with it and matches each of those 3 scenarios separately.

I tried using the 'endswith' but it's too general and can't "get specific" like what i'm looking for in my examples.

Upvotes: 11

Views: 11316

Answers (1)

EoinS
EoinS

Reputation: 5482

Here they are:

string.endswith('.mp4') and string.startswith('/')

string.endswith('.mp4') and "/" in string

string.endswith('.mp4') and "A" not in string

Or, look at using fnmatch.

Upvotes: 19

Related Questions