在 sqlite 或 mysql 中使用正则表达式
我正在使用 sqlite3 来尝试查找拥有 Gmail、Yahoo 或 Hotmail 电子邮件地址的用户。它需要仅根据域名的第一部分来执行此操作,因此我希望接受任何包含 @yahoo 的地址。
从文档中可以看出,在查询 sqlite 数据库时不可能使用正则表达式。有没有优雅的方式来做类似的事情?似乎不可能使用带有多个选项的“like/in”(例如:LIKE (%@yahoo%, %@gmail%, %@hotmail%)?
如果失败,我可能会切换到 MySQL reg exp 因为我想让解决方案保持简单和优雅,并且 DB 不是主要因素,如何在 MySQL 中编写regexp 查询?
I'm using sqlite3 to try and find users who have an e-mail address that is either with Gmail, Yahoo or Hotmail. It needs to do this just on the basis of the first part of the domain, so I want any address that had the @yahoo to be accepted.
It appears from documentation that it is not possible to use a regular expression when querying an sqlite database. Is there any elegant way of doing something similar? It doesn't seem to be possible to use a "like/in" with multiple options (eg: LIKE (%@yahoo%, %@gmail%, %@hotmail%)?
Failing that, I may switch over to MySQL for a reg exp as I want to keep the solution simple and elegant and DB isn't a major factor. How would said regexp query be written in MySQL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能以这种方式使用多个“LIKE”,但您可以使用:
You can't use multiple "LIKE" in that way but you can use:
你可以使用 Nemoden 使用的方式或类似的东西(未测试)
这会快得多,因为
LIKE %string% OR ...
非常慢并且不使用任何索引(不知道 REGEXP 是否使用索引)。you can use the way Nemoden is using or something like this (not tested)
This would be much faster because
LIKE %string% OR ...
is very slow and doesnt use any indexes(dont know if REGEXP uses indexes tho).我想你可能想要这样的东西(RLIKE函数):
如果你使用whatever_cs(区分大小写的排序规则),请使用a-zA-Z而不是AZ。
您还可以删除正则表达式中的 ^ 和 $。 ^ 表示“开始于”,$ 表示“结束于”
更新:
I think you might want something like this (RLIKE function):
If you use whatever_cs (case sensitive collation), use a-zA-Z instead of A-Z.
You can also get rid of ^ and $ in the regex. ^ means "starts with" and $ means "ends with"
UPDATE: