MYSQL Find entries that contain more than 7 numbers - mysql

I need to find entries that contain more than 7 numbers in one of my mysql tables BUT the numbers are separated by letters or anything else.
What I have is this little piece of code I use to find entries like dsc123456789:
select * from crawl where title regexp '[0-9]{7}'
How can I find entries like dsc-123-456_78B9? I tried different things but without success so far.
Thanks

You can use the following solution:
SELECT *
FROM crawl
WHERE title REGEXP '(([^[:digit:]])?[[:digit:]]){8,}';
Why the original query of the answer doesn't work?
-- this query doesn't work!
SELECT *
FROM crawl
WHERE title REGEXP '\d([^\d]?\d){7,}'
MySQL can't use character groups like \d (digits). So the query fails every time. On PHP and other languages the regular expression would look like this:
\d([^\d]?\d){7,}
but on MySQL this isn't valid. So you have to use the character classes of MySQL to solve this:
(([^[:digit:]])?[[:digit:]]){8,}
Hint: Make sure you use {8} or {8,} instead of {7} since you want to find all entries with more than 7 numbers / digits.

Related

Having trouble with MySQL wildcard characters

Im following the instructions on w3schools but something appears to not be working. I keep getting the result "empty set" even though it seems like multiple names should be returned since there is names that start with a,l and c.
BTW I am using MySQL via the terminal on a mac. Thank you.
here is a picture of the table I'm using
Here is my syntax -
mysql> select * from Student where Name like '[alc]%';
With your select you search for a patter that exactly matches '[alc]'
followed by anything denoted by %.
Try regexp like this.
select * from Student where Name regexp '^(a|l|c)';
This will give you names that start with the given letters, not case sensitiv

mysql MATCH AGAINST weird characters query

I have a table where the field "company_name" has weird characters, like "à","ö","¬","©","¬","†", etc. I want to return all "company_name"s that contain these characters anywhere within the string. My current query looks like this:
SELECT * FROM table WHERE
MATCH (company_name) AGAINST ('"Ä","à","ö","¬","©","¬","†"' in natural language mode);
But I keep getting no data from the query. I know this can't be the case, as there are definitely examples of them I can find manually. To be clear, the query itself isn't throwing any errors, just not returning any data.
The minimun word length is 3 pr 4 .
you can change it see manial
https://dev.mysql.com/doc/refman/8.0/en/fulltext-fine-tuning.html
or use regular expressiions
SELECT * FROM table WHERE
ompany_name REGEXP '[Äàö¬©¬†]+';
SELECT *
FROM table
WHERE company_name LIKE '%[^0-9a-zA-Z !"#$%&''()*+,\-./:;<=>?#\[\^_`{|}~\]\\]%' ESCAPE '\'
This will find any wacky stuff, including wide characters or 'euro-ASCII' or emoji.

Isolate an email address from a string using MySQL

I am trying to isolate an email address from a block of free field text (column name is TEXT).
There are many different variations of preceding and succeeding characters in the free text field, i.e.:
email me! john#smith.com
e:john#smith.com m:555-555-5555
john#smith.com--personal email
I've tried variations of INSTR() and SUBSTRING_INDEX() to first isolate the "#" (probably the one reliable constant in finding an email...) and extracting the characters to the left (up until a space or non-qualifying character like "-" or ":") and doing the same thing with the text following the #.
However - everything I've tried so far hasn't filtered out the noise to the level I need.
Obviously 100% accuracy isn't possible but would someone mind taking a crack at how I can structure my select statement?
There is no easy solution to do this within MySQL. However you can do this easily after you have retrieved it using regular expressions.
Here would be a an example of how to use it in your case: Regex example
If you want it to select all e-mail addresses from one string: Regex Example
You can use regex to extract the ones where it does contain an e-mail in MySQL but it still doesn't extract the group from the string. This has to be done outside MySQL
SELECT * FROM table
WHERE column RLIKE '\w*#\w*.\w*'
RLIKE is only for matching it, you can use REGEXP in the SELECT but it only returns 1 or 0 on whether it has found a match or not :s
If you do want to extract it in MySQL maybe this other stackoverflow post helps you out. But it seems like a lot of work instead of doing it outside MySQL
Now in MySQL 5 and 8 you can use REGEXP_SUBSTR to isolate just the email from a block of free text.
SELECT *, REGEXP_SUBSTR(`TEXT`, '([a-zA-Z0-9._%+\-]+)#([a-zA-Z0-9.-]+)\.([a-zA-Z]{2,4})') AS Emails FROM `mytable`;
If you want to get just the records with emails and remove duplicates ...
SELECT DISTINCT REGEXP_SUBSTR(`TEXT`, '([a-zA-Z0-9._%+\-]+)#([a-zA-Z0-9.-]+)\.([a-zA-Z]{2,4})') AS Emails FROM `mytable` WHERE `TEXT` REGEXP '([a-zA-Z0-9._%+\-]+)#([a-zA-Z0-9.-]+)\.([a-zA-Z]{2,4})';

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]

How to make this regex in mysql?

A few min ago I found out that mysql accepts regex, and is great becouse I think it can solve my problem, but I don't know how to write it. So, I need something like this:
SELECT name FROM products WHERE name REGEXP 'regex code'
To give a little more explanation, the name must be in this format: 123425HT and not string99-123425HT. The 123425HT and string99-123425HT is taken arbitrary
Please Help, thanks!
Something like that:
SELECT * FROM t2 WHERE str REGEXP "^([0-9]+)([a-zA-Z]{2})$";
This regexp will found strings which starting from any digits and two small or big characters, for example:
123123hd
12345435MF
6572Sg
If you want use only 6 digits change from [0-9]+ to [0-9]{6}