Patterns

Battle-tested regex, cron and SQL patterns — each one explained, edge-cased, and one click from a live test against your own data.

PatternRegex
Match an email address
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
PatternRegex
Match a web URL
https?:\/\/[\w.-]+(?:\/[\w./?%&=-]*)?
PatternRegex
Match an IPv4 address
\b(?:\d{1,3}\.){3}\d{1,3}\b
PatternRegex
Match a date in YYYY-MM-DD
\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])
PatternRegex
Match a UUID v4
[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}
PatternRegex
Enforce a strong password policy
(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\w\s]).{8,}
PatternRegex
Match a URL slug
^[a-z0-9]+(?:-[a-z0-9]+)*$
PatternRegex
Match a phone number
\+?\d[\d\s().-]{7,}\d
PatternRegex
Match a hex color
#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})\b
PatternRegex
Match a time in 24-hour format
(?:[01]\d|2[0-3]):[0-5]\d
PatternRegex
Match an HTML tag
<\/?[a-zA-Z][\w-]*(?:\s[^<>]*)?>
PatternRegex
Match an ISO 8601 timestamp
\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})
PatternRegex
Match a MAC address
(?:[0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}
PatternRegex
Match a semantic version
\b\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?\b
PatternSQL
Top N per group
SELECT * FROM ( SELECT *, ROW_NUMBER() OVER ( PARTITION BY category ORDER BY price DESC ) AS rn FROM products ) WHERE rn <= 3;
PatternSQL
Deduplicate, keeping the latest row
SELECT * FROM ( SELECT *, ROW_NUMBER() OVER ( PARTITION BY sku ORDER BY updated_at DESC ) AS rn FROM imports ) WHERE rn = 1;
PatternSQL
Running total
SELECT day, amount, SUM(amount) OVER (ORDER BY day) AS running_total FROM daily_sales ORDER BY day;
PatternSQL
Find duplicate rows
SELECT email, COUNT(*) AS copies FROM users GROUP BY email HAVING COUNT(*) > 1 ORDER BY copies DESC;
PatternRegex
Match a currency amount
[₹$€£]\d{1,3}(?:,\d{3})*(?:\.\d+)?
PatternRegex
Match a Markdown link
\[([^\]]+)\]\(([^)\s]+)\)
PatternRegex
Match a hashtag
#[A-Za-z0-9_]+
PatternRegex
Match IP:port
\b(?:\d{1,3}\.){3}\d{1,3}:\d{1,5}\b
PatternRegex
Match a CIDR block
\b(?:\d{1,3}\.){3}\d{1,3}/(?:3[0-2]|[12]?\d)\b
PatternRegex
Match a domain name
\b[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+\b
PatternRegex
Match any UUID
\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\b
PatternRegex
Validate a Base64 string
^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$
PatternRegex
Match a JWT
^eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$
PatternRegex
Match an MD5, SHA-1 or SHA-256 hash
\b[a-fA-F0-9]{64}\b|\b[a-fA-F0-9]{40}\b|\b[a-fA-F0-9]{32}\b
PatternRegex
Match a Windows file path
[A-Za-z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*
PatternRegex
Validate a username
^[a-zA-Z][a-zA-Z0-9_]{2,15}$
PatternRegex
Match a credit card number
^\d(?:[ -]?\d){12,18}$
PatternRegex
Validate a card expiry (MM/YY)
^(?:0[1-9]|1[0-2])/\d{2}$
PatternRegex
Validate a PAN number
^[A-Z]{5}\d{4}[A-Z]$
PatternRegex
Validate a GSTIN
^\d{2}[A-Z]{5}\d{4}[A-Z][1-9A-Z]Z[0-9A-Z]$
PatternRegex
Validate an IFSC code
^[A-Z]{4}0[A-Z0-9]{6}$
PatternRegex
Validate a UK postcode
^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$
PatternSQL
Find orphaned rows
SELECT o.* FROM orders AS o LEFT JOIN customers AS c ON o.customer_id = c.id WHERE c.id IS NULL;
PatternSQL
Rows in one table but not another
SELECT u.* FROM users AS u WHERE NOT EXISTS ( SELECT 1 FROM orders AS o WHERE o.user_id = u.id );
PatternSQL
Pivot rows into columns
SELECT order_month, SUM(CASE WHEN region = 'east' THEN amount ELSE 0 END) AS east, SUM(CASE WHEN region = 'west' THEN amount ELSE 0 END) AS west, SUM(CASE WHEN region = 'north' THEN amount ELSE 0 END) AS north FROM orders GROUP BY order_month;
PatternSQL
Group by month
SELECT date_trunc('month', created_at) AS month, COUNT(*) AS signups FROM users GROUP BY month ORDER BY month;
PatternSQL
Percent of total per row
SELECT product, revenue, revenue * 100.0 / SUM(revenue) OVER () AS pct_of_total FROM product_revenue ORDER BY pct_of_total DESC;
PatternSQL
7-row moving average
SELECT day, amount, AVG(amount) OVER ( ORDER BY day ROWS BETWEEN 6 PRECEDING AND CURRENT ROW ) AS moving_avg_7 FROM daily_sales ORDER BY day;
PatternSQL
Day-over-day change
SELECT day, value, LAG(value) OVER (ORDER BY day) AS previous_value, value - LAG(value) OVER (ORDER BY day) AS day_over_day_change FROM daily_metrics ORDER BY day;
PatternSQL
Median and percentiles
SELECT sensor_id, PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY value) AS median_value, PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER BY value) AS p90_value FROM measurements GROUP BY sensor_id;
PatternSQL
Bucket values into ranges
SELECT CASE WHEN order_total < 100 THEN '0-99' WHEN order_total < 500 THEN '100-499' WHEN order_total < 1000 THEN '500-999' ELSE '1000+' END AS bucket, COUNT(*) AS orders FROM orders GROUP BY bucket ORDER BY MIN(order_total);
PatternSQL
Split a delimited column into rows
SELECT id, unnest(string_split(tags, ',')) AS tag FROM products;
PatternSQL
First and latest value per group
SELECT user_id, min_by(event_name, occurred_at) AS first_event, max_by(event_name, occurred_at) AS latest_event FROM events GROUP BY user_id;
PatternSQL
Count distinct per group
SELECT day, COUNT(*) AS events, COUNT(DISTINCT user_id) AS unique_users FROM events GROUP BY day ORDER BY day;