🧙♂ Regex objesi oluşturulduğunda tekrar tekrar regex tanımlaya gerek yoktur
💠 Regex için Metot Oluşturma
color_regex = re.compile("#([0-9a-fA-F]{6,8})", re.IGNORECASE)defmy_method(content): text = content.group().lower()content = color_regex.sub(my_method, content)
🔍 Arama İşlemleri
result = re.search(r"\[([^\[]+)\]\((.*)\)", "- [name](url)")result[0]# '[name](url)'result[1]# 'name'result[2]# 'url'
👨💻 Renkleri Tersine Çevirme
FILE_PATH ="Buraya kendi yolunu yaz"definvertHex(hexNumber):# invert a hex number inverse =hex(abs(int(hexNumber, 16) -255))[2:]# if the number is a single digit add a preceding zeroiflen(inverse)==1: inverse ='0'+inversereturn inversedefcolorInvert(hexCode):# define an empty string for our new colour code inverse =""# if the code is RGBiflen(hexCode)==6: R = hexCode[:2] G = hexCode[2:4] B = hexCode[4:]# if the code is ARGBeliflen(hexCode)==8: A = hexCode[:2] R = hexCode[2:4] G = hexCode[4:6] B = hexCode[6:]# don't invert the alpha channel inverse = inverse + Aelse:# do nothing if it is neither lengthreturn hexCode inverse = inverse +invertHex(R) inverse = inverse +invertHex(G) inverse = inverse +invertHex(B)return inversedefreplaceHex(matchobj):# exclude the preceding hash symbol of the matched object hexCode = matchobj.group(0)[1:]# invert the colour code and return with the hashreturn'#'+colorInvert(hexCode)filestr =""withopen(FILE_PATH, "r", encoding="utf-8")as file: filestr = file.read()invertedFile = re.sub('#([0-9a-fA-F]{6,8})', replaceHex, filestr)