Python实现的正则表达式文本查找工具
来源:互联网 发布:贵州华为大数据学院 编辑:程序博客网 时间:2024/05/21 22:40
话说,今天接到个任务, 将公司的库移植到arm5上(shit,arm15都有了,你给哥整个arm5….).一顿坑爹骂娘后, 顺利搞出arm5上的库, 开始测试, 然后Bus Error(还不是亲切的 seqmenttation fault)
当然这篇博客不是讲BUS ERROR的前因后果的. 经过各种排查, 发现为此类代码导致int nFuck = *(int *)(shit+42) (恩,库底层有很多这种让人扯蛋的写法),但是这个库在arm7和arm9,android和windows跑的都是很欢的.(是的,坑爹的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斗争吧...
- Python实现的正则表达式文本查找工具
- grep(1) 正则表达式文本查找工具
- 正则表达式如何实现不替换查找的匹配文本?
- 用于查找文本的正则表达式
- 关于notepad++用正则表达式查找文本汉字的分析
- QT:利用正则表达式查找文本
- Python中文文本信息抽取中常见的正则表达式
- 正则表达式在文本工具中的应用
- python工具_正则表达式
- Python 正则表达式查找不是某个字符的字符串
- Python 正则表达式查找带单双引号的URL
- Python 正则表达式查找带有小括号的URL
- python 正则表达式 字符串的 匹配 替换 分割 查找
- Python 正则表达式 字符串的匹配、替换、分割、查找
- 【工具】VS2010查找替换正则表达式
- 正则表达式的工具
- Python 正则表达式限定文本长度
- Python 正则表达式在文本查询URL
- weblogic绿色版安装
- wincc7.2安装授权的时候,该安装哪些?
- 软件工程——软考探究(三)
- VM Depot 镜像新增系列II – 学习管理系统,内容管理系统以及平台管理工具
- Activity切换效果
- Python实现的正则表达式文本查找工具
- 《Effective C++ 》学习笔记——条款03
- 原码, 反码, 补码 详解
- 重定向与转发区别
- Linux 各文件夹的作用
- codeforces Round #260(div2) B解题报告
- Foundation Kit
- HDOJ 1016 Prime Ring Problem
- Four fundamental subspaces of matrix