带方括号的 mySQL 正则表达式

发布于 2024-07-23 11:07:03 字数 1129 浏览 8 评论 0原文

我试图在 mySQL 的列中匹配像 '[sometext]' 这样的字符串(即左方括号、文本、左尖括号、文本、右尖括号、右方括号)。 最初我使用了以下查询(请注意,由于正则表达式查询在 mySQL 中被转义两次,因此您必须在通常使用一个反斜杠的地方使用两个反斜杠):

SELECT * FROM message WHERE msgtext REGEXP '\\[(.+)?<(.+)?>\\]'

该查询没有收到错误,但它返回了我不想要的内容。 我想要 [^\]] (匹配除右方括号之外的所有内容),而不是 (.+)。 当我更改查询时,出现以下错误:“Got error 'repetition-operator operand invalid' from regexp”

阅读 mySQL 文档后 此处,它指出“要包含文字 ] 字符,它必须紧跟在左括号 [.”之后。 由于我想要“^\]”而不是“]”,这是否可能,因为括号不能是左括号之后的第一个字符? 以下是我尝试过的一些查询,这些查询得到了上面列出的相同错误:

SELECT * FROM message WHERE msgtext REGEXP '\\[([^\\]]+?)<([^\\]]+?)>\\]'
SELECT * FROM message WHERE msgtext REGEXP '\\[[^\\]]+?<[^\\]]+?>\\]'
SELECT * FROM message WHERE msgtext REGEXP '\\[[^[.right-square-bracket.]]]+?<[^[.right-square-bracket.]]]+?>\\]'

更新:

以下查询运行时没有错误,但不返回任何行,即使我知道有些列与我正在寻找的内容相匹配(基于我的原始查询位于顶部):

SELECT * FROM message WHERE msgtext REGEXP '\\[([^\\]]+)?<([^\\]]+)?>\\]'

I am trying to match strings like '[sometext<someothertext>]' (i.e., left square bracket, text, left angle bracket, text, right angle bracket, right square bracket) within a column in mySQL. Originally I used the following query (notice that since regex queries are escaped twice in mySQL, you must use two backslashes where you would normally use one):

SELECT * FROM message WHERE msgtext REGEXP '\\[(.+)?<(.+)?>\\]'

This query received no errors, but it returned things I didn't want. Instead of the (.+), I wanted [^\]] (match everything except a right square bracket). When I changed the query, I got the following error: "Got error 'repetition-operator operand invalid' from regexp"

After reading through the mySQL documentation here, it states "To include a literal ] character, it must immediately follow the opening bracket [." Since I want "^\]" instead of "]", is this even possible since the bracket can't be the first character after the opening bracket? Below are some of the queries I have tried which get the same error listed above:

SELECT * FROM message WHERE msgtext REGEXP '\\[([^\\]]+?)<([^\\]]+?)>\\]'
SELECT * FROM message WHERE msgtext REGEXP '\\[[^\\]]+?<[^\\]]+?>\\]'
SELECT * FROM message WHERE msgtext REGEXP '\\[[^[.right-square-bracket.]]]+?<[^[.right-square-bracket.]]]+?>\\]'

UPDATE:

The following query runs without errors, but does not return any rows even though I know there are columns which match what I am looking for (based on my original query at the top):

SELECT * FROM message WHERE msgtext REGEXP '\\[([^\\]]+)?<([^\\]]+)?>\\]'

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

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

发布评论

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

评论(2

第七度阳光i 2024-07-30 11:07:03

这对我有用:

SELECT '[sometext<someothertext>]' REGEXP '\\[([^[.right-square-bracket.]]+)?<([^[.right-square-bracket.]]+)?>\\]
;

This works for me:

SELECT '[sometext<someothertext>]' REGEXP '\\[([^[.right-square-bracket.]]+)?<([^[.right-square-bracket.]]+)?>\\]
;
挽容 2024-07-30 11:07:03

一旦斜线未转义,您的最终正则表达式看起来是正确的并且可以在 Firefox/JS 中运行。 看起来 MySQL 本身并不支持捕获组...也许这就是问题所在。

也许这会很有用: http://mysqludf.com/lib_mysqludf_preg/

另外,您可以尝试使用 * 代替的+? 为你的否定右方。

* 表示 0 次或多次重复(贪婪)
+? 意味着 1 次或多次重复(惰性)

Your final regex looks correct and works in Firefox/JS once the slashes are unescaped. Doesn't look like MySQL supports capture groups natively though... Maybe that's the problem.

Perhaps this would useful: http://mysqludf.com/lib_mysqludf_preg/

Also, you might try a * instead of +? for your negated right squares.

* means 0 or more repetitions (greedy)
+? means 1 or more repetitions (lazy)

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