Mysql regex search with digit - mysql

How would I do the following in mysql?
SELECT * FROM table WHERE search REGEXP '.+season\d+\s?.+' limit 10;
I want to match something like:
"hello this is season1 how are you?"
But not:
"hello this is season1episode1 how are you?

You can use the following regular expression since \d and \s are not available on MySQL. You can use character classes instead.
You can replace \d with [[:digit:]] or [0-9] and \s with [[= =]] or [ ].
SELECT * FROM table WHERE search REGEXP '.+season[[:digit:]]+[[= =]].+' LIMIT 10
-- or...
SELECT * FROM table WHERE search REGEXP '.+season[0-9]+[ ].+' LIMIT 10
demo on dbfiddle.uk

Before MySQL 8.0,
REGEXP "season[0-9]+[[:>:]]"
meaning "season", at least one digit, then a word boundary. Note that it will stop with punctuation.
REGEXP "season[0-9]+[^a-zA-Z]"
Might work for you -- it says that it should be followed by a letter.
8.0 changes the word boundary to:
REGEXP "season[0-9]+\b"
(Caveat: the backslash may need to be doubled up.)

Related

MySQL - query to get all rows that a specific character is non-English

I have a table that has nvarchar elements.
This table has two kinds of elements:
elements with only digit characters
elements with digit characters and the 3rd character is non-English character
I want a query to get all rows that their 3rd character is non-English.
EDIT
use WHERE SUBSTRING(<table>.ColumnName, 3, 1) NOT BETWEEN '0' AND '9' worked for me either
I'd use regexp_like with a regex that the third character isn't a digit:
SELECT *
FROM mytable
WHERE REGEXP_LIKE(mycol, '..[^[:digit:]].*')
In MySQL versions older than 8.0, you could use the regexp operator:
SELECT *
FROM mytable
WHERE mycol REGEXP '..[^[:digit:]].*'
You can use RLIKE operator, below is the query for matching the third character which is not a digit and not an English alphabet
SELECT * FROM
mytable
where SUBSTR(mycol,3,1) NOT RLIKE '^[A-Za-z0-9]$';

MySQL regex for word boundary containing '#'

I'm trying to search for an example phrase: '#test123' using regex like:
SELECT (...) WHERE x RLIKE '[[:<:]]#test123[[:>:]]'
With no luck. Probably the word boundary selector '[[:<:]]' does not count '#' as a word.
How to achieve it? How to set in MySQL regex word boundary selector but with exceptions?
MySQL 5.7 Reference Manual / ... / Regular Expressions:
[[:<:]], [[:>:]]
These markers stand for word boundaries. They match the beginning and
end of words, respectively. A word is a sequence of word characters
that is not preceded by or followed by word characters. A word
character is an alphanumeric character in the alnum class or an
underscore (_).
So, # is a word boundary, not a word character. We need to expand "word characters" class to include # too. The simplest way is to enumerate custom word characters directly a-z0-9_#:
SELECT * FROM
(
SELECT '#test123' AS x UNION ALL
SELECT 'and #test123 too' UNION ALL
SELECT 'not#test123not' UNION ALL
SELECT 'not#test123' UNION ALL
SELECT '#test123not' UNION ALL
SELECT 'not # test123' UNION ALL
SELECT 'test123' UNION ALL
SELECT '#west123'
) t
WHERE x RLIKE '([^a-z0-9_#]|^)#test123([^a-z0-9_#]|$)';
Result:
x
----------------
#test123
and #test123 too
I think you can use below expression instead:
'[.#.][[:<:]]test123[[:>:]]'
Note: don't use non-word literals inside [[:<:]][[:>:]] and use [..] for characters.
Or (with thanks to #Y.B.)
'(^|.*[^a-zA-Z0-9_])[.#.][[:<:]]test123[[:>:]]'

How to use SQL to remove superfluous characters from names?

How do I remove all superfluous full-stop . and semi-colon ; characters from end of last name field values in SQL?
One way to check of the last character is a "full stop" or "semicolon" is to use a substring function to get the last character, and compare that to the characters you are looking for. (There are several ways to do this, for example, using LIKE or REGEXP operator.
If that last character matches, then lop off that last character. One way to do that is to use a substring function. (Use the CHAR_LENGTH function to return the number of characters in the string.)
For example, something like this:
UPDATE mytable t
SET t.last_name = SUBSTR(t.last_name,1,CHAR_LENGTH(t.last_name)-1)
WHERE SUBSTRING(t.last_name,CHAR_LENGTH(t.last_name),1) IN ('.',';')
But, I'd strongly recommend that you test those expressions using a SELECT statement, before running an UPDATE statement.
SELECT t.last_name AS old_val
, SUBSTR(t.last_name,1,CHAR_LENGTH(t.last_name)-1) AS new_val
FROM mytable t
WHERE SUBSTRING(t.last_name,CHAR_LENGTH(t.last_name),1) IN ('.',';')
Substring rows that have a semi-colon or dot :
update emp
set ename = substring(ename, 1, char_length(ename) - 1)
where ename REGEXP '[.;]$';

Regex not working with mysql's REGEXP

Problem:
I want to get all records that contain a subdomain.
Some subdomains are saved prefixed with www. after the http://, but not all are.
Examples:
http://www.sub.domain.com and http://sub.domain.com
I have this working regex that I have tested on RegExr:
^(http:\/\/)(www\.)?(\w)+(\.)(\w)+(.)(\w|\/){2,10}
Which matches both examples nicely.
However when I try using this regex in my query using REGEXP, mysql returns 0 records.
I have tried:
SELECT * FROM `front` WHERE `domain` REGEXP '^(http:\/\/)(www\.)?(\w)+(\.)(\w)+(\.)(\w|\/){2,10}$';
SELECT * FROM `front` WHERE `domain` REGEXP '/^(http:\/\/)(www\.)?(\w)+(\.)(\w)+(\.)(\w|\/){2,10}$/';
SELECT * FROM `front` WHERE `domain` REGEXP '/^(http:\/\/)(www\.)?(\w)+(\.)(\w)+(\.)(\w|\/){2,10}$/g';
Which all return 0 records.
TL;DR
My working REGEX does not seem to be working when used in MySQL's REGEXP function.
There is no \w metacharacter support in MySQL. Use [A-Za-z0-9_] instead:
SELECT * FROM `front` WHERE `domain` REGEXP '^(http:\/\/)(www\.)?([A-Za-z0-9_])+(\.)([A-Za-z0-9_])+(.)([A-Za-z0-9_]|\/){2,10}$';
It's right there in the documentation:
Because MySQL uses the C escape syntax in strings (for example, ā€œ\nā€ to represent the newline character), you must double any ā€œ\ā€ that you use in your REGEXP strings.

search those records which has column value contains % sign in mysql

I want to fetch all records which has one column contained % sign in mysql
we can do this using mysql using like
for ex..
select * from table where column like '%%';
it returns all records..
Please suggest
Use a backslash to escape the percent:
select * from table where column like '%\%%';
will match any row containing a percent character
% is a special character, try using escape characters to find it. Right now you're just telling mysql to look for a string using 2 wildcard characters (%) as opposed to the actual '%' character. Try using
select * from table where column = 'a%' ESCAPE 'a'
Basically telling mySQL to "Look for the string 'a%', but remove the char a in front of it.
EDIT: Another option is just using
select * from table where column = '\%'
Doing the same thing on later mySQL versions. The backslash is the "standard" escape character.
EDIT 2: Or to actually answer your question:
select * from table where column = '%\%%'
You need to escape the literal % sign with a \ e.g.
select * from table where column like '\%%';