How can I use regex word boundary in a MySQL query?
For instance, I want to match either 'product' or 'newsletter',
It works fine with OR,
IF(? = 'product' OR ? = 'newsletter', ... , ...)
But how about a regex? I assume it would be something like this below?
IF(? REGEXP '^('product'||'newsletter')+$', ..., ... )
SELECT *
FROM tbl
WHERE description REGEXP '[[:<:]]red[[:>:]]|[[:<:]]blue[[:>:]]'
or, as a select expression:
SELECT IF(description REGEXP '[[:<:]]red[[:>:]]|[[:<:]]blue[[:>:]]', 1, 0) AS is_matched, tbl.*
FROM description
You can use RLIKE as a synonym but REGEXP seems to be more popular [citation needed].
Case Sensitivity
From the MySQL manual for REGEXP:
REGEXP is not case sensitive, except when used with binary strings.
mysql> SELECT 'a' REGEXP 'A', 'a' REGEXP BINARY 'A';
-> 1 0
Use REGEXP or RLIKE.
Regular expressions in MySQL
In your case, you could use MATCH() ... AGAINST fulltext search with MyISAM storage engine.
Or you could use IN() as #Wiseguy mentioned
Related
I'm using this query in my autocomplete feature:
SELECT description FROM my_table WHERE LIKE "%keyword%"
But this query returns the entire content of the field which is sometimes too long.
Is it possible to limit the number of characters before and after "keyword" ?
I suggest using MySQL's REGEXP operator here. For example, to accept a maximum of 10 characters before and after keyword, you could use:
SELECT description
FROM my_table
WHERE col REGEXP '^.{0,10}keyword.{0,10}$';
Note that if you intend to match keyword as a standalone word, you may want to surround it by word boundaries in the regex pattern:
SELECT description
FROM my_table
WHERE col REGEXP '^.{0,10}\\bkeyword\\b.{0,10}$';
To show for example 5 characters before and after you word you can do it using RIGHT, LEFT and SUBSTRING_INDEX
select description, concat(RIGHT(SUBSTRING_INDEX(description, 'keyword', 1),5), 'keyword', LEFT(SUBSTRING_INDEX(description, 'keyword', -1),5) ) as snippet
from my_table
where description like "%keyword%";
Check it here : https://dbfiddle.uk/MZcVJgEL
I have these rows:
db666405.gallery
db666405.table1
db666405.table2
I want to capture the word after the dot.
How to do with regex in mysql?
I have tried with ^\. or [.], but I did not succeed.
SELECT *
FROM `table`
WHERE `column` REGEXP '^\\.'
MySQL regex doesn't "capture" anything. It's for matching only; there is no support for regex replacement in MySQL.
I assume you want to return the part after the dot:
select *, substring(`column`, instr(`column`, '.') + 1) as ext
from `table`
where `column` like '%.?%'
To select any columns that have the word Gallery, you can use a
SELECT *
FROM `table`
WHERE `column` LIKE '%gallery%' `
This will return all that have gallery
what I have
SELECT CONCAT(`names`,' ',`office`) `bigDataField`
FROM `item_table`
HAVING `bigDataField` REGEXP "jessy|c";
returnes also Data which just contains letter "c" so I would like to ORDER BY most same matching characters, is that possible ?
NOTE: words and characters get changed by user input. So it can be only one character or a few or even a few words.
sql fiddle http://sqlfiddle.com/#!2/dc87e/1
Thanks for all the help
You can order by any expression.
regexp returns the number of matches for the specified regex
So, this:
order by `bigDataField` regexp 'c' desc
will order your data by the bigDataField that has the most c's in it as first so I guess it's not what you are looking for. You can use multiple CASE-WHENs to check the length of the pattern matching (warning: bad performance - not recommended for big tables)
try this
SELECT CONCAT(`names`,' ',`office`) `bigDataField`,
CASE WHEN CONCAT(`names`,' ',`office`) regexp 'jessy' > 0 then length('jessy') * (CONCAT(`names`,' ',`office`) regexp 'jessy') ELSE
CASE WHEN CONCAT(`names`,' ',`office`) regexp 'c' > 0 then length('c') * (CONCAT(`names`,' ',`office`) regexp 'c') ELSE 0 END
END as charmatchcount
FROM `item_table`
HAVING `bigDataField` REGEXP "jessy|c"
ORDER BY charmatchcount desc
To avoid the above ugliness you must use an external library, or create your own function. You may find this thread helpful MySQL - Return matching pattern in REGEXP query
You can try with this
SELECT CONCAT(`names`,' ',`office`) `bigDataField`
FROM `item_table`
HAVING `bigDataField` REGEXP '[a-z] c' order by bigDataField asc;
Hope this will work for you
this works:
SELECT col, IF( LOCATE('Jessy', col) = 0, 0 , LENGTH('Jessy')) as ord
FROM
(
SELECT CONCAT( `names`,' ',`office`) `col`
FROM `item_table`
HAVING `col` REGEXP "jessy|c"
) x
ORDER BY ord DESC;
but if there is 3 word in REGEXP, hard to find relevant query.
BTW are you looking for Full Text Search?
In my table I have firstname and last name. Few names are upper case ( ABRAHAM ), few names are lower case (abraham), few names are character starting with ucword (Abraham).
So when i am doing the where condition using REGEXP '^[abc]', I am not getting proper records. How to change the names to lower case and use SELECT QUERY.
SELECT * FROM `test_tbl` WHERE cus_name REGEXP '^[abc]';
This is my query, works fine if the records are lower case, but my records are intermediate ,my all cus name are not lower case , all the names are like ucword.
So for this above query am not getting proper records display.
I think you should query your database making sure that the names are lowered, suppose that name is the name you whish to find out, and in your application you've lowered it like 'abraham', now your query should be like this:
SELECT * FROM `test_tbl` WHERE LOWER(cus_name) = name
Since i dont know what language you use, I've just placed name, but make sure that this is lowered and you should retrieve Abraham, ABRAHAM or any variation of the name!
Hepe it helps!
Have you tried:
SELECT * FROM `test_tbl` WHERE LOWER(cus_name) REGEXP '^[abc]';
I don't know since when, but nowadays MySql REGEXP is case insensitive.
https://dev.mysql.com/doc/refman/5.7/en/pattern-matching.html
You don't need regexp to search for names starting with a specific string or character.
SELECT * FROM `test_tbl` WHERE cus_name LIKE 'abc%' ;
% is wildcard char. The search is case insensitive unless you set the binary attribute for column cus_name or you use the binary operator
SELECT * FROM `test_tbl` WHERE BINARY cus_name LIKE 'abc%' ;
A few valid options already presented, but here's one more with just regex:
SELECT * FROM `test_tbl` WHERE cus_name REGEXP '^[abcABC]';
I need a MySQL query w/ Regex to tell me if my string's first character is a number from 0 to 9.
The following query returns '1', since the REGEXP matches. You can adapt it for your purposes:
SELECT '123 this starts with a digit' REGEXP '^[[:digit:]]';
You can use it in a SELECT like this:
SELECT * FROM tbl WHERE field REGEXP '^[[:digit:]]';
Use this:
SELECT 'a12' REGEXP '^[0-9]';
=> 0
SELECT '4ab' REGEXP '^[0-9]';
=> 1