MySQL 正则表达式方括号的元字符列表

发布于 2024-12-16 01:48:26 字数 185 浏览 0 评论 0 原文

奇怪的是,我似乎无法在任何地方找到无法在 MySQL 正则表达式方括号内安全地用作文字的字符列表,而无需转义它们或需要使用 [:character_class:]事物。

(而且答案可能需要特定于 MySQL,因为与 Perl/PHP/Javascript 等中的正则表达式相比,MySQL 似乎缺乏正则表达式)。

Strangely I can't seem to find anywhere a list of the characters that I can't safely use as literals within MySQL regular expression square brackets without escaping them or requiring the use of a [:character_class:] thing.

(Also the answer probably needs to be MySQL specific because MySQL regular expressions seem to be lacking compared those in Perl/PHP/Javascript etc).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧人九事 2024-12-23 01:48:26

几乎所有元字符(包括点 .+*? 量词、结尾 -字符串锚点 $ 等)在字符类中没有特殊含义,但有一些值得注意的例外:

  • 右括号 ],出于明显的原因
  • 插入符 ^,用于否定字符类(例如: [^ab] 匹配除 ab 之外的任何字符。
  • 连字符 -,用于表示范围(例如:[0-9] 匹配任何数字)

但是,如果放置在战略位置,仍然可以添加这些字符而不转义在字符类中:

  • 右括号可以放在左括号后面,例如:[]a] 匹配 ]a
  • 插入符号可以放置在除左括号之后之外的任何位置,例如:[a^] 匹配 ^a
  • 连字符可以放在左括号之后或右括号之前,例如:[-a][a-] 都匹配 a-

更多信息可以在 POSIX 的手册页中找到正则表达式(感谢 Tomalak Geret'kal!)

Almost all metacharacters (including the dot ., the +, * and ? quantifiers, the end-of-string anchor $, etc.) have no special meaning in character classes, with a few notable exceptions:

  • closing bracket ], for obvious reasons
  • caret ^, which is used to negate the character class (eg: [^ab] matches any character but a and b).
  • hyphen -, which is used to denote a range (eg: [0-9] matches any digit)

However, these can still be added without escaping if placed in strategic locations within the character class:

  • the closing bracket can be placed right after the opening bracket, eg: []a] matches ] or a.
  • the caret can be placed anywhere but after the opening bracket, eg: [a^] matches ^ or a
  • the hyphen can be placed right after the opening bracket or before the closing bracket, eg: [-a] and [a-] both match a and -.

More information can be found in the man page on POSIX regex (thanks Tomalak Geret'kal!)

寄居人 2024-12-23 01:48:26

来自 文档,靠近顶部:

本节通过示例总结了特殊字符和
可在 MySQL 中用于 REGEXP 操作的构造。 确实如此
不包含亨利·斯宾塞的书中可以找到的所有细节
regex(7) 手册页。该手册页包含在 MySQL 源代码中
发行版,位于 regex 目录下的 regex.7 文件中。

可以找到复制的所述联机帮助页 此处(感谢 Google!)。您要查找的信息都可以在那里找到。

From the documentation, right near the top:

This section summarizes, with examples, the special characters and
constructs that can be used in MySQL for REGEXP operations. It does
not contain all the details that can be found in Henry Spencer's
regex(7) manual page. That manual page is included in MySQL source
distributions, in the regex.7 file under the regex directory.

Said manpage can be found copied here (thanks, Google!). The information you're looking for is available in there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文