Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a string matches a certain pattern?

Tags:

python

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.

like image 610
Gambit2007 Avatar asked Jun 03 '16 15:06

Gambit2007


1 Answers

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.

like image 109
EoinS Avatar answered Oct 20 '22 00:10

EoinS