Got error 'repetition-operator operand invalid' from regexp in Mysql - mysql

I'm having trouble using a regular expression to select some results from my MySQL table.
I'm using this query
select id, orderid, `desc`
from paymentlog
where `desc` REGEXP '[^.]*(?:_SVD(\d*))[[:>:]]'
And it says
#1139 - Got error 'repetition-operator operand invalid' from regexp
This regex works well with my other editors/validators.
Any advice much appreciated.

MySQL regular expressions don't support the full syntax of Perl-Compatible Regular Expressions.
The (?:) grouping syntax is not supported in MySQL. That syntax is for grouping without backreferences. But it doesn't matter in MySQL, since MySQL doesn't support backreferences anyway (related to this, MySQL has no regular expression substitution function).
In fact, you don't need any parens in your example.
As #ruakh says, \d isn't a code to match digits. You need to use POSIX character classes in MySQL.
So your expression should look like this:
where `desc` REGEXP '_SVD[:digit:]*[[:>:]]'
I also left out your [^.]* since it doesn't matter in this expression. You aren't matching the beginning of the string, so zero characters of that class would succeed the match even if you did have a . before the rest of the pattern. You might mean to match only if you have non-dot characters from the start of the string, like this:
where `desc` REGEXP '^[^.]*_SVD[:digit:]*[[:>:]]'

Related

MariaDB search using REGEXP

I am having trouble formulating a valid regular expression for searching in mysql, on a MariaDB database.
I am doing these searches in phpMyAdmin, but will eventually also being doing them in the Search-Replace-DB tool (which is also MySQL based).
Let's say the table I am searching is table-x, and the field is field-y. I need to find the string \root\localhost/cf/.
Here's an example of the data that contains this string:
O:8:"stdClass":2:{s:4:"file";s:58:"\root\localhost/cf//wp-content/wflogs/config-transient.php";s:4:"hash";s:64:"96e5846450f943eeb94993173c13f1dc28a824eff513d1d8019011515b8729f9";}
I tried this regular expression, which works in my expression tester, but doesn't work in MySQL:
\\root\\localhost/cf/. The entire query being,
SELECT * FROM table-x WHERE field-y REGEXP '\\root\\localhost/cf/'.
This gave me the error, #1139 - Got error 'PCRE does not support \L, \l, \N{name}, \U, or \u at offset 6' from regexp.
To get around the \l in my expression, I tried the following:
SELECT * FROM table-x WHERE field-y REGEXP '\\root\\.ocalhost\/cf\/'
This works in my reg exp tester. But it gives no results in phpmyadmin. It did get rid of the PCRE error message though.
What's the correct way to go about this?

Types of Wildcards in MySql

My query:
Select * From tableName Where columnName Like "[PST]%"
is not giving the expected result.
Why does this wildcard not work in MySql?
If you want to filter on strings that contain any 'P', 'S', or 'T', then you can use a regex:
where col rlike '[PST]'
If you want strings that contain substring 'PST', then no need for square brackets - and like is enough:
where col like '%PST%'
If you want the matching character(s) at the start of the string, then the regex solution looks like:
where col rlike '^PST'
And the like option would be:
where col like 'PST%'
MySQL's LIKE syntax is documented here: https://dev.mysql.com/doc/refman/8.0/en/pattern-matching.html
Standard SQL from decades ago defined only two wildcards: % and _. These are the only wildcards an SQL product needs to support if they want to say they are SQL compliant and support the LIKE predicate.
% matches zero or more of any characters. It's analogous to .* in regular expressions.
_ matches exactly one of any character. It's analogous to . in regular expressions.
Also if you want to match a literal '%' or '_', you need to escape it, i.e. put a backslash before it:
WHERE title LIKE 'The 7\% Solution'
Microsoft SQL Server's LIKE syntax is documented here: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/like-transact-sql?view=sql-server-ver15
They support % and _ wildcards, and the \ escape character, but they extend standard SQL with two other forms:
[a-z] matches one character, but only characters in the range inside the brackets. This is similar in regular expressions. The - is a range operator, unless it appears at the start or end of the string inside the brackets.
[^a-z] matches one character, which must not be one of the characters in the range inside the brackets. Also the same in regular expressions.
These are not standard forms of wildcards for the LIKE predicate, and other brands of SQL database don't support them.
Later versions of the SQL standard introduced a new predicate SIMILAR TO which supports much richer patterns and wildcards, since the right-side operand is a string which contains a regular expression. But since this predicate was introduced in a later edition of the SQL standard, some implementations had already developed their own solution that was almost the same.
MySQL called the operator REGEXP and RLIKE is a synonym (https://dev.mysql.com/doc/refman/8.0/en/regexp.html).
It was requested in https://bugs.mysql.com/bug.php?id=746 to support SIMILAR TO syntax to help MySQL comply with the SQL standard, but the request was turned down, because it had subtly different behavior to the existing REGEXP/RLIKE operator.
Microsoft SQL Server has partial support of regular expression wildcards in the LIKE operator, and also a dbo.RegexMatch() function.
SQLite has a GLOB operator, and so on.
Thanks everyone!
For specific this question, we need to use regexp
Select * From tableName Where ColumnName Regexp "^[PST]";
For more detail over Regular Expression i.e Regexp :
https://www.youtube.com/watch?v=KoltE-JUY0c

digit character class in regexp doesn't work in MySQL for me

The following query
SELECT "T2N1M0" REGEXP "^T[:digit:].*";
returns single row with 0 for me.
I would expect it return 1.
What I am doing wrong?
You are missing one level of square brackets []:
SELECT "T2N1M0" REGEXP "^T[[:digit:]].*";
You should have gotten this error message that hints at the problem:
Got error 'POSIX named classes are supported only within a class at offset ' from regexp
More one the syntax for regular expressions are given by the manual page 13.5.2 Regular Expressions.

Mysql regex error #1139 using literal -

I tried running this query:
SELECT column FROM table WHERE column REGEXP '[^A-Za-z\-\']'
but this returns
#1139 - Got error 'invalid character range' from regexp
which seems to me like the - in the character class is not being escaped, and instead read as an invalid range. Is there some other way that it's suppose to be escaped for mysql to be the literal -?
This regex works as expected outside of mysql, https://regex101.com/r/wE8vY5/1.
I came up with an alternative to that regex which is
SELECT column FROM table WHERE column NOT REGEXP '([:alpha:]|-|\')'
so the question isn't how do I get this to work. The question is why doesn't the first regex work?
Here's a SQL fiddle of the issue, http://sqlfiddle.com/#!9/f8a006/1.
Also, there is no language being used here, query is being run at DB level.
Regex in PHP: http://sandbox.onlinephpfunctions.com/code/10f5fe2939bdbbbebcc986c171a97c0d63d06e55
Regex in JS: https://jsfiddle.net/6ay4zmrb/
Just change the order.
SELECT column FROM table WHERE column REGEXP '[^-A-Za-z\']'
#Avinash Raj is correct the - must be first (or last). The \ is not an escape character in POSIX, which is what mysql uses, https://dev.mysql.com/doc/refman/5.1/en/regexp.html.
One key syntactic difference is that the backslash is NOT a metacharacter in a POSIX bracket expression.
-http://www.regular-expressions.info/posixbrackets.html
What special characters must be escaped in regular expressions?
Inside character classes, the backslash is a literal character in POSIX regular expressions. You cannot use it to escape anything. You have to use "clever placement" if you want to include character class metacharacters as literals. Put the ^ anywhere except at the start, the ] at the start, and the - at the start or the end of the character class to match these literally

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

I'm having trouble using a regular expression to select some results from my MySQL table.
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?
According to the MySQL manual
MySQL uses Henry Spencer's implementation of regular expressions, which is aimed at conformance with POSIX 1003.2
POSIX regexes don'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 *?
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[^>]*src="http://www'
Note: The " doesn't have to escaped and the .* at the beginning is implied.
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.
Hope it Helps.
Check Fiddle: http://sqlfiddle.com/#!2/6a2f0/13/0