最后一个朋友为我找到了一个解决方案,不是使用正则表达式而是 python。我在这里分享它以防其他人面临这个问题。这是python代码;它有两个参数:输入和输出文件。
#!/usr/bin/python
import sys
import re
if (len(sys.argv)<3):
print "Try with two arguments."
sys.exit()
FILEIN = sys.argv[1]
FILEOUT = sys.argv[2]
substitutions = { \
"call":"CALL" ,\
"parameter":"PARAMETER" ,\
"allocatable":"ALLOCATABLE" ,\
"dimension":"DIMENSION" ,\
"integer":"INTEGER" ,\
"logical":"LOGICAL" ,\
"double precision":"DOUBLE PRECISION" \
}
patterns = []
for s in substitutions:
patterns.append(["(^[^!]*)\\b%s\\b"%s,"\\1%s"%substitutions[s]])
patterns.append(['("[^"]*)\\b%s\\b([^"]*")'%substitutions[s],"\\1%s\\2"%s])
patterns.append(["('[^']*)\\b%s\\b([^']*')"%substitutions[s],"\\1%s\\2"%s])
patterns.append(["(\([^\)]*)\\b%s\\b([^\)]*\))"%substitutions[s],"\\1%s\\2"%s])
retList = []
f = open(FILEIN,"r")
for line in f:
for p in patterns:
prevLine=""
nextLine="1"
while (prevLine!=nextLine):
nextLine = re.sub(p[0],p[1],line)
prevLine = line
line = nextLine
retList.append(line)
f.close()
f = open(FILEOUT,"w")
f.write(''.join(retList))
f.close()