[英]Remove String between two characters for all occurrences
我正在寻找有关 Python 3 中字符串操作的帮助。
输入字符串
s = "ID bigint,FIRST_NM string,LAST_NM string,FILLER1 string"
期望输出
s = "ID,FIRST_NM,LAST_NM,FILLER1"
基本上,目标是删除输入字符串中所有出现的空格和逗号之间的任何内容。
任何帮助深表感谢
您可以使用正则表达式
import re
s = "ID bigint,FIRST_NM string,LAST_NM string,FILLER1 string"
s = ','.join(re.findall('\w+(?= \w+)', s))
print(s)
输出:
ID,FIRST_NM,LAST_NM,FILLER1
使用简单的regex
import re
s = "ID bigint,FIRST_NM string,LAST_NM string,FILLER1 string"
res = re.sub('\s\w+', '', s)
print(res)
# output ID,FIRST_NM,LAST_NM,FILLER1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.