考虑以下正则表达式
@(.*\..*){2,}
预期行为:
a@b doesnt match
a@b.c doesnt match
a@b.c.d matches
a@b.c.d.e matches
and so on
在正则表达式中测试它按预期工作。
在 mysql select 中使用它无法按预期工作。查询:
SELECT * FROM `users` where mail regexp '@(.*\..*){2,}'
返回类似
的行foo@example.com
不应匹配给定的正则表达式。为什么?
最佳答案
我想你的问题的答案就在这里。
Because MySQL uses the C escape syntax in strings (for example, “\n” to represent the newline character), you must double any “\” that you use in your REGEXP strings.
因为你的中间点没有被正确转义,它被当作另一个通配符,最后你的表达式被有效地折叠为 @.{2,}
或 @..+
@anubhava 的回答可能是你尝试做的更好的替代品,尽管我会注意到@dasblinkenlight 关于使用字符类 [.]
的评论,这将使你很容易放入正则表达式已经在 RegexPal 进行了测试。
关于MySQL 正则表达式匹配至少 2 个点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33695967/