import re # U+202a (LRE) やその他の目に見えない制御文字を除去するための正規表現 # ref: https://www.compart.com/en/unicode/category/Cf INVISIBLE_CHARS_PATTERN = re.compile(r"[\u200b-\u200c\u200e-\u200f\u202a-\u202e\ufeff]") def remove_invisible_chars(text: str) -> str: """ 文字列から目に見えない制御文字やフォーマット文字を除去する。 """ if not isinstance(text, str): return text return INVISIBLE_CHARS_PATTERN.sub("", text)