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 result that may be longer than 1,000,000,000 bytes is an error, each group the template names counted as long as its whole match.
The memory a match takes grows with the size of the pattern times its groups, and its time with the size of the pattern times the length of the text. So a pattern is an error when it is longer than 50,000 bytes (what SQLite takes for LIKE), when it has more than 100 groups, or when, with each repeat written out (x{3} as xxx), it has more than 50,000 characters and operators, or more than 1,048,576 once multiplied by one more than its groups.
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 ¶
Types ¶
This section is empty.