How to use a REGEX in an SQL SELECT query

The following example shows how to use a regular expression (a regex) to search for information in an SQL database.

The regular expression used looks for images which end with #x# – for example test1-200×200.png

SQLRegex1

Make sure to test your regular expression to ensure you get the desired result. There are many free online regex testers, such as: regex101.com

SELECT *
FROM `wp_posts`
WHERE (
`post_content` REGEXP '([0-9]+x[0-9]+).(gif|jpeg|bmp|jpg|png)' and `post_type` = "post"
)

 

Was this article helpful?

Related Articles