【发布时间】:2015-12-04 16:51:46
【问题描述】:
基本上我想编写一个脚本来清理 URL,将点替换为“(点)”字符串。 例如,如果我在运行脚本后有http://www.google.com,我希望它是http://www(dot)google(dot)。 好吧,当我的文本文件仅包含 url 或其他字符串时,使用 .replace 很容易实现,但在我的情况下,我的文本文件中也有 IP 地址,我不希望 IP 地址中的点更改为“ (点)”。
我尝试使用正则表达式来执行此操作,但我的输出是 " http://ww(dot)oogl(dot)om 192.60.10.10 33.44.55.66"
这是我的代码
from __future__ import print_function
import sys
import re
nargs = len(sys.argv)
if nargs < 2:
sys.exit('You did not specify a file')
else:
inputFile = sys.argv[1]
fp = open(inputFile)
content = fp.read()
replace = '(dot)'
regex = '[a-z](\.)[a-z]'
print(re.sub(regex, replace, content, re.M| re.I| re.DOTALL))
我想我需要有一个条件来检查模式是否为 number.number - 不要替换。
【问题讨论】: