常用但记不住的pattern
pattern |
description |
[\u4e00-\u9fa5] |
中文 |
\s |
任何空白字符 |
\S |
任何非空白字符 |
(?=pattern) |
正向肯定预查 |
(?<=pattern) |
反向肯定预查 |
(?!pattern) |
正向否定预查 |
(?<!pattern) |
反向否定预查 |
\w |
中英文数字下划线 |
正向肯定预查
import re
rc = re.compile('小米(?=手机)')
print(rc.fullmatch('小米手机'))
print(rc.fullmatch('小米'))
print(rc.findall('小米手机'))
print(rc.findall('小米粥'))
\w
import re
rec = re.compile('\w')
a = 'aA1啊の.\n,。_+-='
print(rec.findall(a))
print(list(rec.sub('', a)))
特殊字符清洗
import re
a = '''𠙶山aA11,./<>?";':[]{}\\|`~!@#$%^&*()_+-=《》?,。/:;’‘”“【】、·!@=#=¥%=…()—❤'''
rc = re.compile(r'[^-_a-zA-Z\d\u4e00-\u9fa5\s,<.>/?;:"\[{\]}|`~!@#$%^&*()=+,《。》?;:‘’“”【】、·!¥…()—]')
print(rc.findall(a))
rc = re.compile(r'[^-\w\s,<.>/?;:\'"\[{\]}\\|`~!@#$%^&*()=+,《。》?;:‘’“”【】、·!¥…()—]')
print(rc.findall(a))
清除连续空白符
def replace_continuous_blank_lines(text):
"""清除连续空行"""
return re.sub(r'\n\s*\n', '\n', text.strip())
def replace_space(text):
"""清除连续空白"""
text = re.sub(r'\s*\n\s*', '\n', text.strip())
text = re.sub(r'[^\S\n]', ' ', text)
text = re.sub('(?<![\u4e00-\u9fa5]) (?=[\u4e00-\u9fa5])|(?<=[\u4e00-\u9fa5]) (?![\u4e00-\u9fa5])', '', text)
return text
def replace_space_resolutely(text, substitution=''):
return re.sub(r'\s+', substitution, text.strip())
HTML标签清洗
def replace_tag(html, completely=True):
"""替换HTML标签"""
html = re.sub('<img[^>]*>', '', html)
html = re.sub('<br/?>|<br [^<>]*>|<hr/?>|<hr [^<>]*>', '\n'