[Python] 정규표현식으로 특수문자 검출하기

황준하·2022년 12월 29일
0

정규표현식으로 특수문자 검출하는 코드

import re

train_data = [{"sentence_form": "#인생크림+스킨 *_* 최고! @@@"}]

special_set = [",", ";", ":", "!", '"', "?", "+","#", "'", 
             '$','%','&','(',')','-','/', '*', '@', '^', '{', '}','[',']']

s = []

for sp in special_set:
    s.append(re.escape(sp))  # escape 해주어야 정상적으로 동작이 되었음.

s = "".join(s)
s = '[' + s + ']'

reg = re.compile(s)

for t in train_data:
    if len(reg.findall(t['sentence_form'])) !=0:
        print(t['sentence_form'])
    else:
        ;

re.escape 해주어야하는 점을 잊지 말자.

0개의 댓글