Check filename extension is PDF, DOC, DOCX

0
I want to check the filename extension if it's PDF, DOC or DOCX using : isMatch($filename, '(?i).(pdf|docx?)$') But it's always false Any idea
asked
4 answers
3

With The Community commons Regex Test you can test case insensitive as well:

'(?i:^.*.(pdf|docx)$)'

answered
1

isMatch is only true if the pattern matches the whole string.

  • add .* to match the first part of the filename
  • quote the dot as \.
  • no need for the $

this gives

(?i).*\.(pdf|docx?)

answered
0
^.+\.((?:[pP][dD][fF]))$
^.+\.((?:[dD][oO][cC]))$
answered
0

You could also use substring if you do not want to use Regex, find the last " . " and then use substring, you can evaluate it against a string or a list.

Just a thought

answered