带方括号的 mySQL 正则表达式
我试图在 mySQL 的列中匹配像 '[sometext
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这对我有用:
This works for me:
一旦斜线未转义,您的最终正则表达式看起来是正确的并且可以在 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)