How do I search and replace using regex in MySQL? - mysql

I'm trying to update a field which contains HTML and I want to find all the rows that have forms in them and remove the form tags and anything in between them, however I'm running into problems with my select and the regex.
SELECT * FROM db.table WHERE body REGEXP "<form[^>].+?>.+?</form>";
and the error I get says:
'repetition-operator operand invalid' from regexp.
I was hoping to make that SELECT into a subselect for an update query but I'm stuck at this point.

I think your problem is in your form expression. Try the following:
"<form[^>]*>.+?</form>"
Remember that MySQL supports a limited set of regular expression matching and testing.
See this document.

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?

MySQL REGEXP - Where the column contains the regular expression

So I have a table called "lu_regex" with a column called "regex"
Select * from lu_regex;
athedsl-\d+
i5[93][0-9a-fA-F]+\.versa
5ac[a-f0-9]+.+sky
The table contains 1000's of rows, with various Regular Expressions syntax, i'm just showing three in this example.
Now I'm trying to take user input and match that input against the rows in the table. So I'm doing this.
SELECT * FROM lu_regex where '5aca3a11.bb.sky.comr' regexp regex;
regex
5ac[a-f0-9]+.+sky
1 row returned.
I'm getting back what I expected, with that query, then I try this one.
SELECT * FROM lu_regex where 'athedsl-07371.home.otenet.gr' regexp regex;
0 rows returned.
It should match on "athedsl-\d+", but i'm assuming it has something to do with the "\d". I even tried adding this to the database "athedsl-\\d+" and that didn't cause a match either.
I'm trying to stick to a MySQL solution, what am I doing wrong, this should be possible.
I just found this link, it looks like a bug that hasn't been fixed. It was verified in 2013.
https://bugs.mysql.com/bug.php?id=70413
Bug #70413 \d is not working in REGEXP for a MySQL query
I think the solution is going to be is to replace all \d with [0-9]

Regex not supported in sql command with "contains" clause

I am not a seasonal Windows user, I got a task wherein I had to query the Window Index search table i.e "Systemindex" for fetching some user specific data from the db.
And for this I have to match a pattern basically a regular expression while fetching the data.
SELECT System.FileName, System.ItemPathDisplay, System.DateCreated, System.DateModified, System.ItemName, System.KindText FROM Systemindex WHERE Contains('“(?=^[A-Za-z\d!##\$%\^&\*\(\)_\+=]{9,32}$)”');
The above would allow us to search for say stored passwords.
But when I query the db using the below command I was getting an error. And later I came to know that the "contains" clause
does not support regular expression. Is there an alternative to achieve this?
there is REGEXP operator http://dev.mysql.com/doc/refman/5.7/en/regexp.html,
use smth like this
SELECT * FROM Systemindex WHERE some_column REGEXP 'your_regex'

A couple of basic Sql Profiler questions

(Sorry for the longish question, I'll try to be concise.)
I'm running SQL Server Profiler and I'm chasing down some performance issues. I'm relatively new to what the profiler does and I've exported the traces into a table so I can run queries against the data.
One thing I've been running up against is some seemingly odd behavior doing select queries against the TextData field of the table generated by the trace export. It may have to do with the field's data type (ntext, null). I'm selecting for particular values, but getting unexpected results. For example, if I do this:
select * from [TraceAnalyzer].dbo.TraceTable
and I'm interested in values like this:
exec [Sproc_of_interest] #parm1=992
I'd do a query like this:
select * from [TraceAnalyzer].dbo.TraceTable
where TextData like '%exec [Sproc_of_interest] #parm1=%'
but the return result is empty.
Also, if I do a query like:
select * from [TraceAnalyzer].dbo.TraceTable
where TextData like '%exec [Sproc_of_interest]%'
I get unexpected TextData values like exec sp_reset_connection
Would the square brackets in the criteria be messing things up? I've tried omitting them, but that just excludes everything. I'm not aware of escape characters in SQL select queries, but when I copy/paste the value from one of the offending records, the pasted value does not appear to contain anything that would meet the original query's criteria.
Any insights would be greatly appreciated. Thanks.
[Sproc_of_interest] in the pattern syntax is interpreted as matching one character that is in the set S,p,r,o,c,_,o,f,_,i,n,t,e,r,e,s,t.
Three possible ways of solving this are below.
1) Escape [ with square brackets
LIKE '%exec [[]Sproc_of_interest] #parm1=%'
2) Use an escape character
LIKE 'exec \[Sproc_of_interest] #parm1=' ESCAPE '\'
3) Use CHARINDEX instead of escaping anything
WHERE CHARINDEX('exec [Sproc_of_interest] #parm1=' , TextData) > 0

Regexp to validate URL in MySQL

I have tried several regex patterns (designed for use with PHP because I couldn't find any for MySQL) for URL validation, but none of them are working. Probably MySQL has a slightly different syntax.
I've also tried to come up with one, but no success.
So does anyone know a fairly good regex to use with MySQL for URL validation?
According to article 11.5.2. Regular Expressions in MySQL's documentation, you can perform selections with a regular expression with the following syntax
SELECT field FROM table WHERE field REGEX pattern
In order to match simple URLS, you may use
SELECT field FROM table
WHERE field REGEXP "^(https?://|www\\.)[\.A-Za-z0-9\-]+\\.[a-zA-Z]{2,4}"
This will match most urls like
www.google.il
http://google.com/
http://ww.google.net/
www.google.com/index.php?test=data
https://yahoo.dk/as
http://goo.gle.com/
http://wt.a.x24-s.org/ye/
www.website.info
But not
htp://google.com
ww.google.com/
www-google.com
http://google.c
http://goo#.com
httpf://google.com
Although the answer KBA posted works, there are some inconstancies with the escaping.
The proper syntax should be, this syntax works in MySQL as well as in PHP for example.
SELECT field FROM table
WHERE field REGEXP "^(https?:\/\/|www\.)[\.A-Za-z0-9\-]+\.[a-zA-Z]{2,4}"
The above code will only match if the content of 'field' starts with a URL. If you would like to match all rows where the field contains a url (so for example surrounded by other text / content) just simply use:
SELECT field FROM table
WHERE field REGEXP "(https?:\/\/|www\.)[\.A-Za-z0-9\-]+\.[a-zA-Z]{2,4}"