MySQL #1139 - 从正则表达式中得到错误“重复运算符操作数无效”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18317183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:33:59  来源:igfitidea点击:

#1139 - Got error 'repetition-operator operand invalid' from regexp

mysqlregexselect

提问by BackSlash

I'm having trouble using a regular expression to select some results from my MySQL table.

我在使用正则表达式从 MySQL 表中选择一些结果时遇到问题。

I'm using this query

我正在使用这个查询

SELECT text 
FROM `articles` 
WHERE content REGEXP '.*<img.*?src=\"http://www' 
ORDER BY date DESC

And it says

它说

#1139 - Got error 'repetition-operator operand invalid' from regexp

I tested the regex with Notepad++ and it works, why MySQL is giving me this error and how can i fix it?

我用 Notepad++ 测试了正则表达式,它有效,为什么 MySQL 给我这个错误,我该如何解决?

回答by NullUserException

According to the MySQL manual

根据MySQL 手册

MySQL uses Henry Spencer's implementation of regular expressions, which is aimed at conformance with POSIX 1003.2

MySQL 使用 Henry Spencer 的正则表达式实现,旨在符合 POSIX 1003.2

POSIX regexesdon't support using the question mark ?as a non-greedy (lazy) modifier to the star and plus quantifiers like PCRE (Perl Compatible Regular Expressions). This means you can't use +?and *?

POSIX 正则表达式不支持使用问号?作为星号的非贪婪(懒惰)修饰符以及 PCRE(Perl 兼容正则表达式)等量词。这意味着您不能使用+?*?

It looks like you'll just have to use the greedy version, which should still work. To avoid the matching of things like <img style="/*some style*/" src="a.png"> <script src="www.example.com/js/abc.js">, you can use a negated character class:

看起来你只需要使用贪婪的版本,它应该仍然有效。为避免匹配诸如 之类的东西<img style="/*some style*/" src="a.png"> <script src="www.example.com/js/abc.js">,您可以使用否定字符类:

'<img[^>]*src="http://www'

'<img[^>]*src="http://www'

Note: The "doesn't have to escaped and the .*at the beginning is implied.

注意:"不必转义,.*开头是隐含的。

回答by ErickBest

You can try,

你可以试试,

SELECT 
        text 
        , 
     IF (content LIKE '%<img src="http://%', text  , content LIKE '%<img style=%') 
as imageText

FROM    articles ORDER BY date DESC

This will Check first for where content has <img src="http://if it can't find then it will look for <img style=instead.

<img src="http://如果找不到,这将首先检查内容的位置,然后它将查找<img style=

Hope it Helps.

希望能帮助到你。

Check Fiddle: http://sqlfiddle.com/#!2/6a2f0/13/0

检查小提琴:http://sqlfiddle.com/#!2/6a2f0/13/0