简体   繁体   English

将正则表达式模式转换为 sql

[英]Convert Regex pattern to sql

I have a SQL database that contains a table of emails.我有一个包含电子邮件表的 SQL 数据库。 I would like to select all the emails that have characters from the regex pattern:我想选择所有包含正则表达式模式字符的电子邮件:

^[^ @]+@([^ @.]+\\.)+[^ @.]+$

I'm trying to write the request but I have mistakes.我正在尝试编写请求,但我有错误。 Can anyone help me?谁能帮我?

You don't need a regular expression for the pattern you are specifying.您指定的模式不需要正则表达式。 You can use like :你可以使用like

where email like '%@%.%' and
      email not like '%@%@%' and    -- only one @
      email not like '%.%.%' and    -- only one .
      email not like '% %'          -- no space

The use of regular expressions depends on the database you are using (but like is SQL so it should work in all databases).正则表达式的使用取决于您使用的数据库(但like SQL 一样,因此它应该适用于所有数据库)。 Of course, if your (unspecified) database supports regular expression, you can still use them.当然,如果您的(未指定的)数据库支持正则表达式,您仍然可以使用它们。 If you do so, however, you might do some research because there are better regular expressions for email matching.但是,如果您这样做,您可能会做一些研究,因为有更好的正则表达式用于电子邮件匹配。

Here is the example to use RegEx in SQL query这是在 SQL 查询中使用 RegEx 的示例

SELECT * from table WHERE email REGEXP '^[^ @]+@([^ @.]+\.)+[^ @.]+$';

Let me know it works.让我知道它有效。

Thanks all this is the solution that worked for me:谢谢这对我有用的解决方案:

WHERE email !~ '^[^ @]+@([^ @.]+.)+[^ @.]+$' WHERE 电子邮件 !~ '^[^ @]+@([^ @.]+.)+[^ @.]+$'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM