python regex expression image format

I want to limit formatting.

  • The length is 20
  • the name does not contain special characters. like *,'%$^&.@#!~
  • The image format is jpg, jpeg, png, gif, bmp, and webp

so i tried, I wrote it as below. However, I don't know to exclude special symbols. Help me bro!

'^(?!.{20}).*\.(?:gif|png|jpe?g|bmp|webp)$'

2 answers

  • answered 2021-11-29 03:50 aziz k'h

    if you want only file name to be with length 20 (without include extension) :

    ^(?:\w|\s|\d){20}\.(?:jpg|jpeg|png|gif|bmp|webp)$
    

    else

    ^(?=(?:\w|\s|\d)+\.(?:jpg|jpeg|png|gif|bmp|webp)).{20}$
    

  • answered 2021-11-29 07:53 The fourth bird

    You can exclude what you want to allow to match using a negated character class, and repeat that 1-19 times if you want to make it less than 20 characters.

    ^[^*,'%$^&.@#!~\r\n]{1,19}\.(?:gif|png|jpe?g|bmp|webp)$
    

    Regex demo

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum