1

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 '^\\.' 
2
  • so you only want to capture the value preceeding .gallery? Commented Jan 26, 2017 at 18:15
  • NO i want capture the value gallery Commented Jan 26, 2017 at 18:26

2 Answers 2

2

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 '%.?%'
Sign up to request clarification or add additional context in comments.

2 Comments

How does instr differ from locate? Is one more backward/foward compatible?
Compatible in MySQL versions? They probably exist in all versions. Compatible with other RDBMS products? I doubt it; cross-vendor compatibility anywhere in SQL is an unfulfilled dream.
0

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

4 Comments

sorry this is only an example but i have more row that the word gallery change like this db666405.gallery, db666405.table1, db666405.table2 . i want to capture the word after the dot
YOu can use a site such as regex101 to test regexes. ^.+\.(.+)$ will work because the parenthesis say to start after the period on what is captured
You can't capture in mysql.
^.+\.(.+)$ It works with PHP but with MySQL does not work $re = '/^.+\.(.+)$/'; <br> $str = 'db666405.gallery'; preg_matchl($re, $str, $matches); // Print the entire match result print_r($matches);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.