问题描述
奇怪的是,我似乎无法在任何地方找到我无法安全地用作MySQL正则表达式方括号内的文字的字符列表,而不会转义它们或要求使用[:character_class:]
东西.
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.
(答案也可能需要特定于MySQL,因为与Perl/PHP/Javascript等相比,MySQL正则表达式似乎缺乏).
(Also the answer probably needs to be MySQL specific because MySQL regular expressions seem to be lacking compared those in Perl/PHP/Javascript etc).
推荐答案
几乎所有元字符(包括点.
,+
,*
和?
量词,字符串结尾锚$
等)在字符类中没有特殊含义,但有一些值得注意的例外:
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:
- 右方括号
]
, - 插入符号
^
,用于否定字符类(例如:[^ab]
匹配任何字符但a
和b
). - 连字符
-
,用于表示范围(例如:[0-9]
匹配任何数字)
- closing bracket
]
, for obvious reasons - caret
^
, which is used to negate the character class (eg:[^ab]
matches any character buta
andb
). - 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:
- 可以将闭合括号放在打开括号之后,例如:
[]a]
匹配]
或a
. - 插入符可以放置在左括号之后的任何位置,例如,例如:
[a^]
匹配^
或a
- 连字符可以放在左括号或右括号之前,例如:
[-a]
和[a-]
都与a
和-
匹配.
- the closing bracket can be placed right after the opening bracket, eg:
[]a]
matches]
ora
. - the caret can be placed anywhere but after the opening bracket, eg:
[a^]
matches^
ora
- the hyphen can be placed right after the opening bracket or before the closing bracket, eg:
[-a]
and[a-]
both matcha
and-
.
更多信息可以在 POSIX的手册页中找到. regex
(感谢Tomalak Geret'kal!)
More information can be found in the man page on POSIX regex
(thanks Tomalak Geret'kal!)
这篇关于MySQL正则表达式方括号的元字符列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!