我有这样的字符串

mystring = "CBS Network Radio Panel;\ntitle2 New York OCT13W4, Panel Weighting;\n*options; mprint ls=max mprint;\n\n****************************************out; asd; ***hg;"


我想删除*和;之间的字符串。
输出应该是

"CBS Network Radio Panel;\ntitle2 New York OCT13W4, Panel Weighting;\ mprint ls=max mprint;\n\n asd;"


我已经尝试过此代码

re.sub(r'[\*]*[a-z]*;', '', mystring)


但这不起作用。

最佳答案

您可以使用

re.sub(r'\*[^;]*;', '', mystring)


请参见Python demo

import re
mystring = "CBS Network Radio Panel;\ntitle2 New York OCT13W4, Panel Weighting;\n*options; mprint ls=max mprint;\n\n****************************************out; asd; ***hg;"
r = re.sub(r'\*[^;]*;', '', mystring)
print(r)


输出:

CBS Network Radio Panel;
title2 New York OCT13W4, Panel Weighting;
 mprint ls=max mprint;

 asd;


r'\*[^;]*;'模式与文字*匹配,后跟零个或多个除;以外的字符,然后是;

关于python - 删除python中特殊字符之间的字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39223859/

10-12 16:46
查看更多