Please explain me why I get different results with using re.find and re.sub
请解释为什么使用re.find和re.sub会得到不同的结果
The string which I parse:
我解析的字符串:
GRANT USAGE ON *.* TO 'testuser'@'10.10.10.10' IDENTIFIED BY PASSWORD '*A78AF560CD6F8FEA4DC8205299927B6CB1B1F56A'
Code:
代码:
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))
So I get what I want:
所以我得到了我想要的:
['testuser']
['10.10.10.10']
So, I want to change ip address and user, so I try to use re.sub
我想改变ip地址和用户,所以我尝试使用re.sub
Code
代码
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)
But I just get this:
但是我得到了这个:
another_ip
another_user
1 个解决方案
#1
3
With re.sub()
you need to specifically target which part of the string are you trying to substitute. In other words, re.sub()
would replace everything that was matched by a regular expression (well, strictly speaking, the leftmost non-overlapping occurrence of a pattern
) - in your case you are replacing the complete string. Instead, you can match the user and the IP address specifically, for example:
对于re.sub(),您需要明确指定要替换的字符串的哪一部分。换句话说,re.sub()将替换所有与正则表达式匹配的内容(严格地说,是模式最左边的不重叠出现)——在您的示例中,您将替换完整的字符串。相反,您可以专门匹配用户和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'"
#1
3
With re.sub()
you need to specifically target which part of the string are you trying to substitute. In other words, re.sub()
would replace everything that was matched by a regular expression (well, strictly speaking, the leftmost non-overlapping occurrence of a pattern
) - in your case you are replacing the complete string. Instead, you can match the user and the IP address specifically, for example:
对于re.sub(),您需要明确指定要替换的字符串的哪一部分。换句话说,re.sub()将替换所有与正则表达式匹配的内容(严格地说,是模式最左边的不重叠出现)——在您的示例中,您将替换完整的字符串。相反,您可以专门匹配用户和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'"