错误:File "C:\Anaconda3\lib\sre_compile.py", line 764, in compile

在python中出现错误为C:\Anaconda3\python.exe G:/Python/YinZhongKai/two.py
Traceback (most recent call last):
File “G:/Python/YinZhongKai/two.py”, line 48, in
about = re.sub(pattern,string1,string) #python中,正则模范式如果出现了中文的符号,会报错
File “C:\Anaconda3\lib\re.py”, line 192, in sub
return _compile(pattern, flags).sub(repl, string, count)
File “C:\Anaconda3\lib\re.py”, line 286, in _compile
p = sre_compile.compile(pattern, flags)
File “C:\Anaconda3\lib\sre_compile.py”, line 764, in compile
p = sre_parse.parse(p, flags)
File “C:\Anaconda3\lib\sre_parse.py”, line 930, in parse
p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
File “C:\Anaconda3\lib\sre_parse.py”, line 426, in _parse_sub
not nested and not items))
File “C:\Anaconda3\lib\sre_parse.py”, line 819, in _parse
source.tell() - start)
re.error: missing ), unterminated subpattern at position 15

Process finished with exit code 1

正则表达式:pattern=r’(黑客)|(抓包)|(监听)|(Trojan)’ 最后的“)”为中文,故出错,所以需要改为英文状态下的“)”’
#正则表达式替换 sub
import re
pattern=r’(黑客)|(抓包)|(监听)|(Trojan)’ #正则表达式模版 在r’ ‘中,汉字等不需要加引号,
string=‘我的梦想是当一名黑客,做一个有素质的抓包者。’ #需要正则的字符串
string1=’@_@’
about = re.sub(pattern,string1,string) #python中,正则模范式如果出现了中文的符号,会报错
print(about)
就不会出错

你可能感兴趣的:(个人在python中的见解)