Home > Database > Mysql Tutorial > Why Does My MySQL REGEXP Query Fail with \'repetition-operator operand invalid\'?

Why Does My MySQL REGEXP Query Fail with \'repetition-operator operand invalid\'?

Barbara Streisand
Release: 2024-11-26 15:54:11
Original
615 people have browsed it

Why Does My MySQL REGEXP Query Fail with

Failed Regular Expression: 'repetition-operator operand invalid'

When attempting to execute the following MySQL query:

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

you encounter the error: #1139 - Got error 'repetition-operator operand invalid' from regexp. Despite the regular expression functioning correctly in Notepad , MySQL rejects it.

Understanding the Issue: POSIX vs. PCRE

The MySQL regular expression engine adheres to POSIX 1003.2, which lacks support for the question mark (?) as a non-greedy modifier in quantifiers ( and ). Consequently, you cannot employ ? and ? in your regular expressions.

Resolution: Utilizing a Greedy Quantifier

To resolve this issue, employ the greedy version of the quantifier, which will still suffice for your intended purpose. Nevertheless, to prevent undesired matching of elements like:

<img>
Copy after login

incorporate a negated character class as follows:

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

Remember that the " character does not necessitate escaping and that the .* at the beginning is implicit.

The above is the detailed content of Why Does My MySQL REGEXP Query Fail with \'repetition-operator operand invalid\'?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template