使用python查找Java代码里面的中文,忽略注释
这次遇到个问题,项目要国际化,因此要把代码里面的中文查找出来,并且忽略注释,由于对python比较熟,所以第一时间想到用python解决这个问题,废话不多说,直接上代码
#读取Java文件中的中文,忽略注释
def findChineseCharacterFromJavaFileIgnoreComment(path) :
#匹配中文
pattern = re.compile(chanieseRegPatten)
#返回结果
result=[]
#匹配文件中的注释包括单行注释和块注释
ignorePatten=re.compile("/\*{1,2}[\s\S]*?\*/|//[\s\S]*?\n")
file = open(path, encoding="UTF-8")
strfile=file.read()
#删除Java代码里面的注释
rel=re.sub(ignorePatten,"",strfile)
#遍历字符串,一次处理一行
for line in rel.splitlines():
rel111 = pattern.findall(line)
if len(rel111) != 0:
print(rel111)
file.close()
return result
path=input("input path :")
findChineseCharacterFromJavaFileIgnoreComment(path)