Files
Python_CookBook_repo/2.字符串和文本/1.任意多分隔符字符串拆分.py
2025-09-10 16:12:45 +08:00

23 lines
657 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 众所周知split函数可以针对单个分隔符把字符串拆开
a = "suka,caonimade,shabi"
b = a.split(",")
print(b)
# 但是假如系统可以检测到这种粗鄙之语,那就会让🐎🐎消失
# 想要保卫自己的🐎,那就要粗鄙的隐晦一点:
a = "suka,*&&**caonimade,_&^shabi"
# 这时候关键词检测就暴毙了
b = a.split(",")
print(b)
# 但是聪明的审核会用re库来解决这个问题
import re
# re库中的split函数接受多个输入可以同时干掉多种干扰
b = re.split(r'[,*&^_]', a)
print(b)
# 如果要保留分隔符, 记得使用正则闭包
b = re.split(r'(,|&|^|_)', a)
print(b)