In this HackerRank Hex Color Code problem solution, You are given N lines of CSS code. Your task is to print all valid Hex Color Codes, in order of their occurrence from top to bottom.

HackerRank Hex Color Code in python problem solution

HackerRank Hex Color Code in python problem solution

import re,string
m = int(raw_input())
check = False
regexp = r'\#[0-9A-Fa-f]+'
for i in xrange(m):
    line = raw_input()
    if(line):
        startIndex = 0
        endIndex = len(line)
        if(check == False and line.find('{')!=-1):
            startIndex = line.find('{')
            check = True
        if(check == True and line.find('}') != -1):
            endIndex = line.find('}')
            line = line[startIndex:endIndex]
            for color in re.findall(regexp,line):
                if(len(color)==4 or len(color)==7):
                    index = line.index(color)
                    if(line[index+len(color)] in string.ascii_letters or line[index+len(color)] in string.digits):
                        continue
                    else:
                        print color
            check = False
        if check:
            line = line[startIndex:endIndex]
            for color in re.findall(regexp,line):
                if(len(color)==4 or len(color)==7):
                    index = line.index(color)
                    if(line[index+len(color)] in string.ascii_letters or line[index+len(color)] in string.digits):
                        continue
                    else:
                        print color