mysql - 正则表达式匹配 MySQL 中的重复/别名电子邮件
问题描述
我正在尝试提出一些正则表达式来检查数据库中是否存在电子邮件。这里更具体的是,我想找到相同但可能写法不同的电子邮件。eg与和john.doe@example.com
相同。johndode+123@example.com
j.o.h.n.d.o.e@example.com
使用另一个脚本,我删除了所有的点和文本,+
这样 john.doe+123@example.com
就变成了johndoe@example.com
. 如何使用这个“剥离”地址来匹配它在 MySQL 数据库中的别名?
我对 RegEx 几乎没有经验,而且我之前也没有真正将它与 SQL 一起使用过。无论如何,我只能想出以下代码:
RegEx(匹配@ 符号之前的+<any text>
所有内容.
(\+.*(?=@)|\.(?=.*@))
SQL
SELECT email FROM users WHERE email REGEXP '(\+.*(?=@)|\.(?=.*@))'
//or
SELECT * from users WHERE email REGEXP_LIKE('johndoe@example.com', '(\+.*(?=@)|\.(?=.*@))')
我得到以下两个错误:
#1064 - 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以了解在附近使用的正确语法
REGEXP_LIKE('johndoe@example.com', '(\+.*(?=@)|\.(?=.*@))')
我想做的是select email from users where email = 'johndoe@example.com' but disregard these characters (\+.*(?=@)|\.(?=.*@)
(代码作为准备好的语句执行)
任何提示或建议将不胜感激!
解决方案
在 MySQL 中,最简单的方法可能是将问题分解为用户名和域:
where replace(substring_index(substring_index(email, '@', 1), '+', 1), '.', '') = substring_index('johndoe@example.com', '@', 1) and
substring_index(email, '@', -1) = substring_index('johndoe@example.com', '@', -1) and
email like '%@%' and
email not like '%@%@%'
推荐阅读
- django - 覆盖 Django 管理界面的查询集
- python - 如何使 VS 代码 IntelliSense 仅显示类属性而不显示构建对象属性?
- google-earth-engine - 错误:.select(...).sampleRegions 不是函数 - 如何解决?
- matplotlib - Matplotlib:带标记的等高线图线样式
- html - 引导菜单卡住
- .net - 获取内部站点以使用 Azure 应用代理
- c# - 如何取消配置部分项的前缀?
- react-native - React Native 中的 Jest 和异步存储
- c - 我在 C 中创建了一个推送和弹出函数,但不知道如何按顺序打印它们
- dynamics-crm-365 - CRM365:图像确实显示在统一接口的列中