jed

Pattern matching

jed offers four ways to match text against a pattern: SQL LIKE / ILIKE, the SQL-standard SIMILAR TO, the regular-expression operators ~ ~* !~ !~*, and the regexp_* functions (regexp_replace, regexp_match, regexp_matches, the split functions, and the regex and SIMILAR forms of substring).

LIKE / ILIKE

LIKE is the SQL wildcard match: % matches any run of characters, _ matches exactly one. ILIKE is the case-insensitive form. The match is by Unicode code point.

Ctrl/⌘ + Enter to run · in-memory

No results yet. Run a query to see output.

To match a literal % or _, escape it. The default escape character is backslash (\%, \_, \\). ESCAPE picks a different one, which is handy when the pattern itself contains backslashes; ESCAPE '' turns escaping off. The escape must be a single character (22025 otherwise).

Ctrl/⌘ + Enter to run · in-memory

No results yet. Run a query to see output.

SIMILAR TO

s SIMILAR TO pattern is TRUE when the pattern matches the whole string. % and _ are LIKE’s wildcards, and |, *, +, ?, {n,m}, ( ), and [...] work as in a regular expression. ., ^, and $ are ordinary characters. NOT SIMILAR TO is the negation.

Ctrl/⌘ + Enter to run · in-memory

No results yet. Run a query to see output.

As in PostgreSQL, the pattern is translated into a regular expression, which similar_to_escape(pattern [, escape]) shows. The default escape character is backslash, ESCAPE picks another (it must be a single character, 22025 otherwise), and ESCAPE '' turns escaping off. % and _ also match a newline.

Ctrl/⌘ + Enter to run · in-memory

No results yet. Run a query to see output.

Regular expressions

The ~ operator is TRUE when the pattern matches somewhere in the subject (it is unanchored — anchor with ^ / $ for a whole-string match). ~* is case-insensitive, and !~ / !~* are their negations.

jed’s regex flavor is its own, deliberately not PostgreSQL-compatible: it is a clean RE2-style subset run by a linear-time engine, so it is immune to catastrophic-backtracking (“ReDoS”) attacks — a pattern that would hang a backtracking engine runs in linear time here. Backreferences and lookaround are intentionally absent.

Ctrl/⌘ + Enter to run · in-memory

No results yet. Run a query to see output.

The pattern surface: literals, . (any code point except newline), character classes [...] / [^...], the shorthands \d \w \s (and \D \W \S), anchors ^ $ and the string-boundary escapes \A (start) / \z (absolute end), alternation |, groups (...) / (?:...), and the quantifiers * + ? {n} {n,} {n,m} (each with a lazy ?-suffixed form). Matching is greedy and leftmost-first.

Case-insensitive matching:

Ctrl/⌘ + Enter to run · in-memory

No results yet. Run a query to see output.

Negation — accounts whose address has no + tag:

Ctrl/⌘ + Enter to run · in-memory

No results yet. Run a query to see output.

Linear-time by construction

The classic catastrophic-backtracking pattern (a+)+$ is harmless here — it matches (or fails) in time proportional to the input, never exponentially:

Ctrl/⌘ + Enter to run · in-memory

No results yet. Run a query to see output.

regexp_replace

regexp_replace(source, pattern, replacement [, flags]) replaces the first match (or all matches with the g flag). The replacement is a template: \1…\9 splice in capture groups, \& the whole match, and \\ a literal backslash.

Ctrl/⌘ + Enter to run · in-memory

No results yet. Run a query to see output.

Reordering with capture groups:

Ctrl/⌘ + Enter to run · in-memory

No results yet. Run a query to see output.

regexp_match

regexp_match(source, pattern [, flags]) returns a text[] of the first match’s capture groups (or the whole match when the pattern has no group), or NULL when there is no match.

Ctrl/⌘ + Enter to run · in-memory

No results yet. Run a query to see output.

substring(… FROM pattern)

SUBSTRING(source FROM pattern), or the function form substring(source, pattern), returns the text of the first match: the first capture group when the pattern has one, otherwise the whole match, and NULL when nothing matches. A quoted second argument is a pattern, as in PostgreSQL, so substring('hello' FROM '2') searches for 2; pass an integer (FROM 2) for the position form. Large captures work the same way: the example returns all 150,000 characters and reports their length. Configured query resource limits still apply.

Ctrl/⌘ + Enter to run · in-memory

No results yet. Run a query to see output.

substring(… SIMILAR pattern ESCAPE e)

The SQL-standard form SUBSTRING(source SIMILAR pattern ESCAPE e), also written SUBSTRING(source FROM pattern FOR e), takes a SIMILAR TO pattern that must match the whole string, with two e" markers around the part to return. The part before the first marker matches as little as possible and the marked part as much as possible, as in PostgreSQL. It returns NULL when the pattern does not match.

Ctrl/⌘ + Enter to run · in-memory

No results yet. Run a query to see output.

regexp_matches

regexp_matches(source, pattern [, flags]) is the set-returning form: one text[] row per match, each shaped like regexp_match’s result. Without the g flag it returns at most one row (zero rows when there is no match); with g it returns every match. It is a table function, so call it in FROM (a FROM t CROSS JOIN regexp_matches(t.col, …) runs it once per row of t).

Ctrl/⌘ + Enter to run · in-memory

No results yet. Run a query to see output.

regexp_split_to_table / regexp_split_to_array

These split the source at every match of the pattern. regexp_split_to_table returns one row per piece, and regexp_split_to_array returns the pieces as a text[]. As in PostgreSQL, an empty match at the start or end of the string, or right after the previous match, does not split, so the empty pattern '' splits a string into its characters.

Ctrl/⌘ + Enter to run · in-memory

No results yet. Run a query to see output.

Ctrl/⌘ + Enter to run · in-memory

No results yet. Run a query to see output.

The i flag makes any of these case-insensitive. g makes regexp_replace replace every match and regexp_matches return every match. The other functions reject it with 22023, as PostgreSQL does. A malformed pattern raises 2201B (invalid_regular_expression).

jed — an embeddable, strictly-typed SQL database.