PostgreSQLdata validation

Validate Hex Color Code in PostgreSQL Using Regex

Check hex color code format in PostgreSQL using regex operators. SQL examples for validating CSS color values in a database.

The Regex Pattern

Regex pattern

^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$

Use this pattern with PostgreSQL's ~ operator.

PostgreSQL SQL Example

-- Validate hex color values stored in a settings or themes table in PostgreSQL using the regex operator
SELECT *
FROM your_table
WHERE your_column ~ '^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$';

How This Works

Validate hex color values stored in a settings or themes table in PostgreSQL using the regex operator. The regex pattern is used with PostgreSQL's ~ operator to filter rows at the database level.

Using regex directly in SQL is efficient for use cases like data validation โ€” it avoids loading data into application memory just to filter it with a regex.

Need a custom SQL + Regex query?

Describe your specific use case in plain English. RegSQL generates the exact PostgreSQL query with the right regex pattern for your schema.

Related SQL Patterns