regexp

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package regexp gives SQLite the REGEXP operator and regexp functions, with Go's regexp syntax (RE2: no backreferences, and time linear in the text).

sqlite.NewConnector(dsn, regexp.Option())

Then:

x REGEXP pattern                       1 if pattern matches somewhere in x, else 0
regexp_like(x, pattern)                the same
regexp_substr(x, pattern)              the first match, or NULL
regexp_replace(x, pattern, repl)       x with every match replaced

repl takes Go's syntax, not \1: $1 or ${1} is a group by number, ${name} a group by name, and $$ a dollar. A name runs as far as letters, digits and _ go, so write ${1}x, not $1x (a group named 1x). A group that is not there gives the empty text.

A NULL argument gives NULL. A REAL argument is an error: write CAST(x AS TEXT). A pattern that does not compile is the error of the statement.

Example
package main

import (
	"database/sql"
	"fmt"

	"github.com/liustack/sqlite"
	"github.com/liustack/sqlite/ext/regexp"
)

func main() {
	c, err := sqlite.NewConnector(":memory:", regexp.Option())
	if err != nil {
		panic(err)
	}
	db := sql.OpenDB(c)
	defer db.Close()
	var match bool
	var first, swapped string
	err = db.QueryRow(`SELECT 'v1.22.3' REGEXP '^v\d+\.\d+\.\d+$',
		regexp_substr('order 66 and 99', '\d+'),
		regexp_replace('Doe, Jane', '(\w+), (\w+)', '$2 $1')`).Scan(&match, &first, &swapped)
	if err != nil {
		panic(err)
	}
	fmt.Println(match, first, swapped)
}
Output:
true 66 Jane Doe

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Option

func Option() sqlite.Option

Option adds the operator and the functions to a connector.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL