所以我有一个文件,我想排队并替换一些非常相似的单词,实际上其中一些是其他单词的子串:
uint8=("UINT8 ","PIN_UINT8 ")
uint16=("UINT16 ","PIN_UINT16 ")
uint32=("UINT32 ","PIN_UINT32 ")
uint64=("UINT64 ","PIN_UINT64 ")
int8=("INT8 ","PIN_INT32 ")
int16=("INT16 ","PIN_INT16 ")
int32=("INT32 ","PIN_INT32 ")
int64=("INT64 ","PIN_INT64 ")
这些是替代品。但我需要小心,因为INT8
是UINT8
的子字符串,所以即使我有匹配,我也需要检查它是UINT8
还是INT8
。
我写了一些代码,但还远未奏效。
while pos!=-1:
print line[pos:]
if "INT8 " in line[pos:]:
pos=line.find("INT8",pos_final)
if "U" == line[pos-1]:
line = line.rstrip().replace(*uint8)
else:
print line[pos-1]
line = line.rstrip().replace(*int8)
pos+=5 #len(PIN_)+1
elif "INT16 " in line[pos:]:
pos=line.find("INT16",pos_final)
if "U" == line[pos-1]:
line = line.rstrip().replace(*uint16)
else:
print line[pos-1]
line = line.rstrip().replace(*int16)
pos+=5
elif "INT32 " in line[pos:]:
pos=line.find("INT32",pos_final)
if "U" == line[pos-1]:
line = line.rstrip().replace(*uint32)
else:
print line[pos-1]
line = line.rstrip().replace(*int32)
pos+=5
elif "INT64 " in line[pos:]:
pos=line.find("INT64",pos_final)
if "U" == line[pos-1]:
line = line.rstrip().replace(*uint64)
else:
print line[pos-1]
line = line.rstrip().replace(*int64)
pos+=5
else:
pos =-1
这里有一些文件行,因此您可以知道响应是否正确并且有效:
UINT32 CacheSize() const { return _cacheSize; } UINT32 LineSize() const { return _lineSize; } UINT32 Associativity(UINT64 obj, INT8 obj2) const { return _associativity; } VOID SplitAddress(const ADDRINT addr, CACHE_TAG & tag, UINT32 & setIndex) const VOID SplitAddress(const ADDRINT addr, CACHE_TAG & tag, UINT32 & setIndex, UINT32 & lineIndex) const { const UINT32 lineMask = _lineSize - 1;
答案 0 :(得分:1)
你可以使用re模块,尝试下面的代码
import re
text = """UINT32 CacheSize() const { return _cacheSize; }
UINT32 LineSize() const { return _lineSize; }
UINT32 Associativity(UINT64 obj, INT8 obj2) const { return _associativity; }
VOID SplitAddress(const ADDRINT addr, CACHE_TAG & tag, UINT32 & setIndex) const
VOID SplitAddress(const ADDRINT addr, CACHE_TAG & tag, UINT32 & setIndex, UINT32 & lineIndex) const
{
const UINT32 lineMask = _lineSize - 1 INT8;
"""
for line in text.split("\n"):
line = re.sub("UINT8 ","PIN_UINT8 ", line)
line = re.sub("UINT16 ","PIN_UINT16 ", line)
line = re.sub("UINT32 ","PIN_UINT32 ", line)
line = re.sub("UINT64 ","PIN_UINT64 ", line)
line = re.sub("[^UPIN_]INT8 ","PIN_INT32 ", line)
line = re.sub("[^UPIN_]INT16 ","PIN_INT16 ", line)
line = re.sub("[^UPIN_]INT32 ","PIN_INT32 ", line)
line = re.sub("[^UPIN_]INT64 ","PIN_INT64 ", line)
print "\t\t",line
<强>输出强>
PIN_UINT32 CacheSize() const { return _cacheSize; }
PIN_UINT32 LineSize() const { return _lineSize; }
PIN_UINT32 Associativity(PIN_UINT64 obj,PIN_INT32 obj2) const { return _associativity; }
VOID SplitAddress(const ADDRINT addr, CACHE_TAG & tag, PIN_UINT32 & setIndex) const
VOID SplitAddress(const ADDRINT addr, CACHE_TAG & tag, PIN_UINT32 & setIndex, PIN_UINT32 & lineIndex) const
{
const PIN_UINT32 lineMask = _lineSize - 1PIN_INT32 ;
答案 1 :(得分:1)
我假设您的文件中没有INT7
。
import re
text = """UINT32 CacheSize() const { return _cacheSize; }
UINT32 LineSize() const { return _lineSize; }
UINT32 Associativity(UINT64 obj, INT8 obj2) const { return _associativity; }
VOID SplitAddress(const ADDRINT addr, CACHE_TAG & tag, UINT32 & setIndex) const
VOID SplitAddress(const ADDRINT addr, CACHE_TAG & tag, UINT32 & setIndex, UINT32 & lineIndex) const
{
const UINT32 lineMask = _lineSize - 1 INT8;
"""
for line in text.split("\n"):
print re.sub(r'(U?INT\d+?)',r'PIN_\1', line)