gpt4 book ai didi

MySQL 正则表达式匹配至少 2 个点

转载 作者:行者123 更新时间:2023-11-29 04:18:20 25 4
gpt4 key购买 nike

考虑以下正则表达式

@(.*\..*){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.

MYSQL Reference

因为你的中间点没有被正确转义,它被当作另一个通配符,最后你的表达式被有效地折叠为 @.{2,}@..+

@anubhava 的回答可能是你尝试做的更好的替代品,尽管我会注意到@dasblinkenlight 关于使用字符类 [.] 的评论,这将使你很容易放入正则表达式已经在 RegexPal 进行了测试。

关于MySQL 正则表达式匹配至少 2 个点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33695967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com