Python - How to check if string is a HEX Color Code -


i need help!

im kinda new whats regarding python , "hex".

i have site people can enter own hex-color messages.

so have built in except "ishexcolor" check..

i realy dont know how can sure user gave valid hex-color-code.

currently im checking if len 6 digits, nothing more.

if not len(color) == 6: return error("hex-color" must contain 6 characters") 

can might me? , should work python 2.5!

you use regular expression:

^#(?:[0-9a-fa-f]{3}){1,2}$ 

for using regular expression in python https://docs.python.org/2/library/re.html

import re str = '#ffffff' # hex  match = re.search(r'^#(?:[0-9a-fa-f]{3}){1,2}$', str)  if match:                         print 'hex valid'  else:   print 'hex not valid' 

Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -