Home > Blockchain >  python regex expression image format
python regex expression image format

Time:11-30

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)$'

CodePudding user response:

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}$

CodePudding user response:

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

  • Related