我喜欢替换新的字符串'MEMORY247'代替0.0MB,2GB,0Gb ...字符串为此我尝试在RE下面但它给出了错误的结果请告诉我正确使用python的正则表达式
b = re.sub(“\ d。(\ s \ w + b)”,“MEMORY247”,“0.0Mb是不够的2GB是多0gb空间3.4gb')
感谢 Mukthar
答案 0 :(得分:1)
你走了:
re.sub("\d+\.*\d*\w*", 'MEMORY247', '0.0Mb is not enough 2GB is much 0gb more space 3.4gb')
这意味着:
这个正则表达式更好:
"\d+\.*\d*[kKmMgG][bB]"
答案 1 :(得分:1)
尝试以下
>>>st='0.0Mb is not enough 2GB is much 0gb more space 3.4gb'
>>>re.sub('(\d+\.*\d*((MB)|(GB)))','MEMORY247',st,flags=re.IGNORECASE)
>>>'MEMORY247 is not enough MEMORY247 is much MEMORY247 more space MEMORY247'
>>>
您正在搜索的模式是
使匹配大小写不敏感,将re.IGNORECASE作为标志传递