st = """
What kind of speCialist would we see for this?He also seems to have reactions to the red dye cochineal/carmine cialist,I like Cialist much
"""
这里我只需要替换Cialist字符串(完全匹配),也可以在末尾添加逗号
不应抛出“spe * cialist *”这个词
我尝试了这个正则表达式。
bold_string = "<b>"+"Cialist"+"</b>"
insensitive_string = re.compile(re.escape("cialist"), re.IGNORECASE)
comment = insensitive_string.sub(bold_string,st)
但它也会抛出字符串专家。
你能建议我解决这个问题吗?
在python中替换十六进制字符还有一个问题。
date_str = "28-06-2010\xc3\x82\xc2\xa008:48 PM"
date_str = date_str.replace("\n","").replace("\t","").replace("\r","").replace("\xc3\x82\xc2\xa"," ")
date_obj = datetime.strptime(date_str,"%d-%m-%Y %H:%M %p")
Error: time data '08-09-2005\xc3\x82\xc2\xa010:18 PM' does not match format '%d-%m-%Y %H:%M %p'
这里我无法用空格替换十六进制字符以匹配日期时间模式。
你可以帮忙解决这个问题吗?
答案 0 :(得分:0)
使用\ b匹配单词边界。然后变得简单:)
import re
st = """
What kind of speCialist would we see for this?He also seems to have reactions to the red dye cochineal/carmine cialist,I like Cialist much
"""
print re.sub(r'\bCialist\b', "<b>Cialist</b>", st)
对于第二个问题,你在最后一个替换字符串的末尾错过了一个0。只需添加0就可以了:)
date_str = "28-06-2010\xc3\x82\xc2\xa008:48 PM"
print date_str
date_str = date_str.replace("\n","").replace("\t","").replace("\r","").replace("\xc3\x82\xc2\xa0"," ")
print repr(date_str)
答案 1 :(得分:0)
一个中有两个问题?
用字边界替换正则表达式,使其为re.sub(r'\bcialist\b', '', your_string, re.I)
答案 2 :(得分:0)
第二个问题:
>>> re.sub(r'\\[a-zA-z0-9]{2}', lambda L: str(int(L.group()[2:], 16)), text)
'28-06-20101238212210008:48 PM'
要么为你的strptime重新组织,要么对它进行解释。