我正在尝试在 SELECT 语句中使用 REGEX 从 MySQL 数据库中提取 IP 地址。当我在 MySQL 控制台中运行查询时,它返回预期结果:
SELECT * FROM database.table WHERE field3 REGEXP '^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$';
当我通过 python 脚本运行相同的查询时,它返回的结果不是 IP 地址。
query = ("SELECT * FROM database.table WHERE field3 REGEXP '^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$';")
我哪里出错了?
最佳答案
Marc B. 成功了。我会引用他的评论。
\. probably. one of those backslashes will be parsed away by python, leaving only . when it arrives at mysql, which then gets parsed as "this is a dot, not a single-char wildcard". \. directly in mysql will be parsed as backslash followed by dot – Marc B
关于python - 在 Python 脚本 MySQL 查询中使用 RegEx 进行 IP 地址验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40313389/