需要從這個串 str1 = '<\11.1.1.1\testdata>'
逃逸「」字符在python串[需要避免六編碼]
當下列選項實現 1. reg = re.compile("^.*\/+([\d\.]+)/\+.*$",re.I).search mth = reg(str2) mth.group(1)
得到的錯誤消息接IP地址
Traceback (most recent call last):
File "<pyshell#90>", line 1, in <module>
mth.group(1)
AttributeError: 'NoneType' object has no attribute 'group'
選項2.
str1 = "<\11.1.1.1\cisco>"
str1.replace("\\","\\\\")
print str1
output - '<\t.1.1.1\\\\cisco>'
試圖使STR1作爲原料串
str1 = r"<\11.1.1.1\cisco>" str2 = str1.replace("\\","/"); print str2 output - '</11.1.1.1/cisco>'
reg = re.compile("^.*\/+([\d\.]+)/\+.*$",re.I).search mth = reg(str2) mth.group(1)
'
error message -
Traceback (most recent call last):
File "<pyshell#90>", line 1, in <module>
mth.group(1)
AttributeError: 'NoneType' object has no attribute 'group'
您試圖原始字符串...但只'str1'而不是在正規表達? – TigerhawkT3