File size: 513 Bytes
b1ec9f2
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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)