python re.findall vs re.sub

时间:2016-04-05 15:05:50

标签: python regex

请解释一下为什么我使用re.find和re.sub

会得到不同的结果

我解析的字符串:

GRANT USAGE ON *.* TO 'testuser'@'10.10.10.10' IDENTIFIED BY PASSWORD '*A78AF560CD6F8FEA4DC8205299927B6CB1B1F56A'

代码:

import re

S="GRANT USAGE ON *.* TO 'testuser'@'10.10.10.10' IDENTIFIED BY PASSWORD '*A78AF560CD6F8FEA4DC8205299927B6CB1B1F56A'"

U=re.compile(r'.* TO \'(.*?)\'@.*')
H=re.compile(r'.*\'@\'(.*?)\'.*')

print(U.findall(S))
print(H.findall(S))

所以我得到了我想要的东西:

['testuser']  
['10.10.10.10']

所以,我想更改IP地址和用户,所以我尝试使用re.sub

代码

import re
S="GRANT USAGE ON *.* TO 'testuser'@'10.10.10.10' IDENTIFIED BY PASSWORD '*A78AF560CD6F8FEA4DC8205299927B6CB1B1F56A'"

U=re.compile(r'.* TO \'(.*?)\'@.*')
H=re.compile(r'.*\'@\'(.*?)\'.*')

HOST=H.sub('another_ip',S) 
USER=U.sub('another_user',S)
print(HOST)
print(USER)

但我得到了这个:

another_ip
another_user

1 个答案:

答案 0 :(得分:3)

使用re.sub(),您需要专门定位要替换的字符串的哪个部分。换句话说,re.sub()将替换正则表达式匹配的所有内容(好吧,strictly speakingthe leftmost non-overlapping occurrence of a pattern) - 在您的情况下,您将替换完整的字符串。相反,您可以专门匹配用户和IP地址,例如:

>>> re.sub(r"'(\w+)'@'(\d+\.\d+\.\d+\.\d+)'", "'another_user'@'another_ip'", S)
"GRANT USAGE ON *.* TO 'another_user'@'another_ip' IDENTIFIED BY PASSWORD '*A78AF560CD6F8FEA4DC8205299927B6CB1B1F56A'"