Home > OS >  Allowing files with proper names using Regex
Allowing files with proper names using Regex

Time:04-21

Here are different files and the output which should look like:

  1. abc.csv - Correct
  2. abc.csv.gz - Fail
  3. abc.csv.csv - Fail
  4. abc.def.csv - Correct

The present regex looks like .*[.]csv$ which passes cases 1 and 2, but not 3 and 4. I tried using {} and \B to allow only 1 extension but wasn't able to make it correctly.

CodePudding user response:

As said in the comments, filename.endswith('.csv') is in my opinion the most pythonic option.

That said, if you want to use a regex, you can use re.match('.*(?<!\.csv)(\.csv)$', s) or re.fullmatch('.*(?<!\.csv)(\.csv)', s)

s = 'abc.csv.csv'
re.match('.*(?<!\.csv)(\.csv)$', s)
# no match

s = 'abc.def.csv'
re.match('.*(?<!\.csv)(\.csv)$', s)
#<re.Match object; span=(0, 11), match='abc.def.csv'>

regex demo

  • Related