Python实现的正则表达式文本查找工具

来源:互联网 发布:贵州华为大数据学院 编辑:程序博客网 时间:2024/05/21 22:40

    话说,今天接到个任务将公司的库移植到arm5(shit,arm15都有了,你给哥整个arm5….).一顿坑爹骂娘后顺利搞出arm5上的库开始测试然后Bus Error(还不是亲切的 seqmenttation fault)

    当然这篇博客不是讲BUS ERROR的前因后果的. 经过各种排查发现为此类代码导致int nFuck = *(int *)(shit+42) (,库底层有很多这种让人扯蛋的写法),但是这个库在arm7arm9,androidwindows跑的都是很欢的.(是的,坑爹的arm5和指针强转)

    解决方案是memcpy,(当然用结构体整体赋值那是最好的,但是没自动化测试工具的库,你是不敢动它的)

   Ok,之前都是吐槽,问题来了.这种指针强转的写法分布之广,令人发指.且摆着各种姿势在代码深处等着((int *) ,( int   *) , (int*) ).普通的查找方法肯定是歇菜的不过SourceInsight还是很牛的,有正则表达式查找方法,于是我屁颠屁颠的就把\(\s*int\s*\*+\s*\)输进去了结果“int *pclass, long omax)”这种货色都给我匹配出来了(小学没毕业不能这么玩我吧),各种测试,感觉的SourceInsight自带的正则表达式查找方法跟我是尿不到一个壶里去了

  仔细想想,这个正则表达式查找方法实现起来其实蛮简单的,一个遍历目录和文件的递归方法:iterDir , 对搜索到的每个文件,调用方法: searchFile,遍历文件里的每一行,对每一行调用方法: checkLine,判断改行能否匹配用户输入的表示式,如果匹配成功, 调用方法:writeLineInfo将结果写入文件.

  于是,就写了个python脚本,代码如下:

# -*- coding: gb2312 -*-

"""@author: VxPython

"""

import os

import re

class SearchClass(object):

def __init__(self , strPath = "" , strExpression=""):

super(self.__class__ , self).__init__()

self.strOutFileName = "Result.log"

self.bSkipNote = True

self.strPath = strPath

self.strExpression = strExpression

self.printInfo = None

self.initUI()

#有时间用PyQt实现个GUI界面

def initGuiUI(self):

pass

def initCuiPrint(self , strInfo):

print strInfo

def initCuiUI(self):

self.printInfo = self.initCuiPrint

def initUI(self):

self.initCuiUI()

#递归遍历目录

def iterDir(self , path , depth):

#depth 递归深度

if os.path.isdir(path):

liPath = os.listdir(path)

nFileNum = len(liPath)

nCount = 1

for strChildPath in liPath:

strShow = "iter %s:Process:%d / %d" % (path , nCount , nFileNum )

self.printInfo(strShow)

self.iterDir("/".join([path , strChildPath]) , depth+1)

os.system("cls") #linux "clear"

nCount += 1

elif os.path.isfile(path):

self.searchFile( path)

else :

self.printInfo('What Fuck File Type...\n"%s" skip\n' % path )

def initWork(self):

self.strPath = raw_input("Enter Dir Path==>")

self.strExpression = raw_input("Enter Expression==>")

self.printInfo('Use "%s" in "%s"...\n' % (self.strExpression , self.strPath) )

try:

self.hOutFile = open(self.strOutFileName , "w")

except Exception , reason:

self.printInfo(reason)

def endWork(self):

try:

self.hOutFile.close()

except Exception , reason:

self.printInfo(reason)

self.printInfo("Work Over")

def runMan(self):

while True:

self.initWork()

self.iterDir(self.strPath , 0)

self.endWork()

strRes = raw_input("Search Again(y|Y)==>")

if not strRes.upper() == "Y":

break;

#将用户输入的表达式开始匹配,是的,就是这么简单粗暴

def checkLine(self , strLine):

search = re.search(self.strExpression , strLine)

if None == search : return False

else :

##跳过注释行,其它语言的注释自己看着办,对于多行注释暂时不实现了

if self.bSkipNote:#看SourceInsight有这功能,实现个简单的

match = re.match("\s*[/\*|//].*", strLine)

if match != None:

return False

return True

def writeLineInfo(self , strFilePath , strLine , nLineCount):

try:

self.hOutFile.write("%s:%d--%s" % (strFilePath , nLineCount , strLine))

except Exception , reason:

self.printInfo(reason)

def searchFile(self , strFilePath):

nLineCount = 0

with open(strFilePath , 'r') as hInFile:

for strLine in hInFile:

nLineCount +=1

if self.checkLine(strLine):

self.writeLineInfo(strFilePath , strLine , nLineCount)

else:

pass

if __name__ == "__main__":

#clsSearch = SearchClass("D:\Hello.txt" , "\(\s*int\s*\*+\s*\)")

clsSearch = SearchClass()

clsSearch.runMan()


然后, 继续苦逼的跟bus error斗争吧...

0 0
原创粉丝点击