Skip to content

Commit

Permalink
Document regexp_replace
Browse files Browse the repository at this point in the history
Document the usage of the `regexp_replace` function.
  • Loading branch information
alendit committed May 6, 2024
1 parent 9190a3c commit fed9c9b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions website/docs/releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ TODO: Update release number (#TODO) in database version [database version 4](hyp
* Introduced new [database file format version 4](hyper-api/hyper_process#version-4) to support reading and persisting the new 32-bit floats.
* A `CAST(… AS double precision)` is needed to store such columns in older file formats.
* Documented the new and improved [database file format version 3](hyper-api/hyper_process#version-3) that was introduced in version 0.0.16123. The new format supports 128-bit numerics. Refer to [Hyper Database Settings](/docs/hyper-api/hyper_process#default_database_version) for more information.
* Documented the [regexp_replace](sql/scalar_func/string_matching#regex-functions) function which provides substitution of new text for substrings based on POSIX regular expressions.

:::warning
Queries using `REAL`, `FLOAT4`, or `FLOAT(p)` with `p <= 24` are now treated as 32-bit floating points.
Expand Down
17 changes: 16 additions & 1 deletion website/docs/sql/scalar_func/string_matching.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ Some examples:
'abc' ~ '^a' → true
'abc' ~ '(b|d)' → true
'abc' ~ '^(b|c)' → false

## Regular Expression Functions {#regex-functions}

The `regexp_replace` function provides substitution of new text for substrings that match POSIX regular expression patterns. It has the syntax `regexp_replace(source, pattern, replacement[, flags ])`. The `source` string is returned unchanged if there is no match to the pattern. If there is a match, the `source` string is returned with the replacement string substituted for the matching substring.

The replacement string can contain `\N`, where `N` is `1` through `9`, to indicate that the source substring matching the `N`'th parenthesized subexpression of the pattern should be inserted. Write `\\` if you need to put a literal backslash in the replacement text.

`pattern` is searched from the beginning of the string. By default, only the first match of the pattern is replaced. If the `g` flag is given, then all matches at or after the start position are replaced. The `i` flag enables case-insensitive matching. Flags can be combined in a single string.

Some examples:

regexp_replace('foobarbaz', 'b..', 'X') → 'fooXbaz'
regexp_replace('foobarbaz', 'b..', 'X', 'g') → 'fooXX'
regexp_replace('foobarbaz', 'b(..)', 'X\1Y', 'g') → 'fooXarYXazY'
regexp_replace('A PostgreSQL function', 'a|e|i|o|u', 'X', 'gi') → 'X PXstgrXSQL fXnctXXn'

## Regular Expression Syntax {#regex-syntax}

Expand Down Expand Up @@ -140,4 +155,4 @@ constraints are:
Constraint |Description
------------|----------------------------------------
`^` |matches at the beginning of the string
`$` |matches at the end of the string
`$` |matches at the end of the string

0 comments on commit fed9c9b

Please sign in to comment.