### Install sqlite-regex Package
Source: https://github.com/asg017/sqlite-regex/blob/main/README.md
Instructions for installing the sqlite-regex library using package managers for different programming languages. This ensures the library is available for use in your projects.
```python
pip install sqlite-regex
```
```bash
datasette install datasette-sqlite-regex
```
```bash
npm install sqlite-regex
```
```bash
gem install sqlite-regex
```
```bash
cargo add sqlite-regex
```
--------------------------------
### Install sqlite-regex NPM Package
Source: https://github.com/asg017/sqlite-regex/blob/main/npm/sqlite-regex/README.md
Installs the sqlite-regex package using npm. This command downloads the package and its dependencies, including platform-specific pre-compiled SQLite extensions.
```bash
npm install sqlite-regex
```
--------------------------------
### Install datasette-sqlite-regex Plugin
Source: https://github.com/asg017/sqlite-regex/blob/main/python/datasette_sqlite_regex/README.md
Installs the datasette-sqlite-regex plugin using the datasette CLI. This command downloads and configures the plugin, making the sqlite-regex extension available to Datasette.
```bash
datasette install datasette-sqlite-regex
```
--------------------------------
### Install sqlite-regex Python Package
Source: https://github.com/asg017/sqlite-regex/blob/main/python/sqlite_regex/README.md
Installs the sqlite-regex package using pip. This is the primary method for incorporating the regex extension into Python projects.
```bash
pip install sqlite-regex
```
--------------------------------
### Get Path to sqlite-regex Loadable Extension (Python)
Source: https://github.com/asg017/sqlite-regex/blob/main/python/sqlite_regex/README.md
Retrieves the full file system path to the installed sqlite-regex loadable extension. This path can be used with `sqlite3.Connection.load_extension()` if manual loading is required.
```python
import sqlite_regex
path = sqlite_regex.loadable_path()
print(path)
# Example output: '/path/to/your/venv/lib/pythonX.Y/site-packages/sqlite_regex/regex0'
```
--------------------------------
### Load SQLite Extension and Create Corpus Table
Source: https://github.com/asg017/sqlite-regex/blob/main/benchmarks/dates/README.md
This snippet demonstrates loading the 'lines0' extension in SQLite and then creating a table named 'corpus'. The table is populated with lines read from the './input-text.txt' file using the 'lines_read' function provided by the loaded extension. This is a foundational step for text processing within SQLite.
```sql
.load lines0
create table corpus as
select line
from lines_read('./input-text.txt');
```
--------------------------------
### Load sqlite-regex Extension in Deno
Source: https://context7.com/asg017/sqlite-regex/llms.txt
Provides an example of loading the sqlite-regex extension in Deno using the `deno.land/x/sqlite3` and `deno.land/x/sqlite_regex` modules. It enables extensions and fetches the version.
```typescript
import { Database } from "https://deno.land/x/sqlite3@0.8.0/mod.ts";
import * as sqlite_regex from "https://deno.land/x/sqlite_regex/mod.ts";
const db = new Database(":memory:");
db.enableLoadExtension = true;
sqlite_regex.load(db);
const [version] = db.prepare("select regex_version()").value<[string]>()!;
console.log(version);
// 'v0.2.4-alpha.1'
```
--------------------------------
### Publish Datasette with datasette-sqlite-regex Plugin
Source: https://github.com/asg017/sqlite-regex/blob/main/python/datasette_sqlite_regex/README.md
Publishes a Datasette instance to Google Cloud Run while installing the datasette-sqlite-regex plugin. This ensures the regex extension is available in the deployed Datasette service.
```bash
datasette publish cloudrun data.db --service=my-service --install=datasette-sqlite-regex
```
--------------------------------
### Get sqlite-regex version
Source: https://github.com/asg017/sqlite-regex/blob/main/docs.md
Returns the semantic versioning (semver) string of the currently loaded sqlite-regex extension.
```sql
select regex_version();
-- "v0.1.0"
```
--------------------------------
### Load sqlite-regex Extension into Python sqlite3 Connection
Source: https://github.com/asg017/sqlite-regex/blob/main/python/sqlite_regex/README.md
Demonstrates how to load the sqlite-regex extension into a Python sqlite3 connection object. It shows how to get the path to the loadable extension and then load it using the provided `load` function.
```python
import sqlite_regex
import sqlite3
conn = sqlite3.connect(':memory:')
sqlite_regex.load(conn)
result = conn.execute('select regex_version(), regex()').fetchone()
print(result)
# Expected output: ('v0.1.0', '01gr7gwc5aq22ycea6j8kxq4s9')
```
--------------------------------
### Get sqlite-regex debug information
Source: https://github.com/asg017/sqlite-regex/blob/main/docs.md
Returns a debug string containing detailed information about the sqlite-regex extension, including its version, build date, and commit hash.
```sql
select regex_debug();
/*
Version: v0.0.0-alpha.4
Source: 85fd18bea80c42782f35975351ea3760d4396eb6
*/
```
--------------------------------
### Get sqlite-regex Loadable Path (node-sqlite3)
Source: https://github.com/asg017/sqlite-regex/blob/main/npm/sqlite-regex/README.md
Obtains the absolute path to the sqlite-regex extension, intended for use with `node-sqlite3`'s `loadExtension` method. The path is dynamically determined based on the operating system and CPU architecture.
```javascript
import sqlite3 from "sqlite3";
import * as sqlite_regex from "sqlite-regex";
const db = new sqlite3.Database(":memory:");
db.loadExtension(sqlite_regex.getLoadablePath());
```
--------------------------------
### Get sqlite-regex Loadable Path (better-sqlite3)
Source: https://github.com/asg017/sqlite-regex/blob/main/npm/sqlite-regex/README.md
Retrieves the absolute path to the compiled sqlite-regex extension, suitable for use with `better-sqlite3`'s `loadExtension` method. This function determines the correct path based on the host's OS and architecture.
```javascript
import Database from "better-sqlite3";
import * as sqlite_regex from "sqlite-regex";
const db = new Database(":memory:");
db.loadExtension(sqlite_regex.getLoadablePath());
```
--------------------------------
### Find All Occurrences of a Pattern in a String with sqlite-regex
Source: https://github.com/asg017/sqlite-regex/blob/main/README.md
Shows how to find all occurrences of a regular expression pattern within a given string using `regex_find_all`. It returns the start and end positions, along with the matched substring.
```sql
select regex_find(
'[0-9]{3}-[0-9]{3}-[0-9]{4}',
'phone: 111-222-3333'
);
-- '111-222-3333'
select rowid, *
from regex_find_all(
'\b\w{13}\b',
'Retroactively relinquishing remunerations is reprehensible.'
);
/*
┌───────┬───────┬─────┬───────────────┐
│ rowid │ start │ end │ match │
├───────┼───────┼─────┼───────────────┤
│ 0 │ 0 │ 13 │ Retroactively │
│ 1 │ 14 │ 27 │ relinquishing │
│ 2 │ 28 │ 41 │ remunerations │
│ 3 │ 45 │ 58 │ reprehensible │
└───────┴───────┴─────┴───────────────┘
*/
```
--------------------------------
### Extract Named Capture Groups from Multiple Matches (SQL)
Source: https://github.com/asg017/sqlite-regex/blob/main/docs.md
This example demonstrates extracting named capture groups ('title', 'year') from multiple occurrences of a pattern within a text using `regex_captures` and `regex_capture`. It also shows how to handle non-existent capture groups, which will result in empty values.
```sql
select
rowid,
captures,
regex_capture(captures, 'title') as title,
regex_capture(captures, 'year') as year,
regex_capture(captures, 'blah') as blah
from regex_captures(
regex("'\(?P
[^']+'\)\s+\((?P\d{4})\)"),
"'Citizen Kane' (1941), 'The Wizard of Oz' (1939), 'M' (1931)."
);
```
--------------------------------
### Extract Multiple Capture Groups using regex_captures and regex_capture (SQL)
Source: https://github.com/asg017/sqlite-regex/blob/main/docs.md
This example demonstrates using `regex_captures` to find all non-overlapping matches and then `regex_capture` to extract specific named groups ('title', 'year') from each match. It handles cases where a named group might not exist, returning an empty string.
```sql
select
regex_capture(captures, 'title') as title,
regex_capture(captures, 'year') as year,
regex_capture(captures, 'not_exist') as not_exist
from regex_captures(
regex("'\(?P[^']+'\)\s+\((?P\d{4})\)"),
"'Citizen Kane' (1941), 'The Wizard of Oz' (1939), 'M' (1931)."
);
```
--------------------------------
### Get Version and Debug Info with regex_version() and regex_debug()
Source: https://context7.com/asg017/sqlite-regex/llms.txt
The regex_version() function returns the current version string of the sqlite-regex extension. The regex_debug() function provides more detailed debug information, including the version, source commit hash, and potentially other internal state details useful for troubleshooting.
```sql
-- Get version string
select regex_version();
-- 'v0.2.4-alpha.1'
-- Get detailed debug info
select regex_debug();
/*
Version: v0.2.4-alpha.1
Source: 85fd18bea80c42782f35975351ea3760d4396eb6
*/
```
--------------------------------
### Get All Matching Patterns with regexset_matches() Table Function
Source: https://context7.com/asg017/sqlite-regex/llms.txt
The regexset_matches() function is a table function that returns all patterns from a RegexSet object that successfully match the input text. For each match, it provides the 'key' (index of the pattern in the set) and the 'pattern' string itself. This is useful for identifying all applicable categories or rules.
```sql
-- Find which patterns match
select key, pattern
from regexset_matches(
regexset(
'\w+',
'\d+',
'\pL+',
'foo',
'bar',
'barfoo',
'foobar'
),
'foobar'
);
/*
┌─────┬─────────┐
│ key │ pattern │
├─────┼─────────┤
│ 0 │ \w+ │
│ 2 │ \pL+ │
│ 3 │ foo │
│ 4 │ bar │
│ 6 │ foobar │
└─────┴─────────┘
*/
-- Categorize content based on matching patterns
select
doc_id,
group_concat(pattern, ', ') as matched_categories
from documents, regexset_matches(
regexset(
'(?i)technology',
'(?i)finance',
'(?i)sports',
'(?i)health'
),
documents.content
)
group by doc_id;
```
--------------------------------
### Find All Regex Matches with regex_find_all()
Source: https://context7.com/asg017/sqlite-regex/llms.txt
The regex_find_all() function returns all non-overlapping matches of a regular expression pattern within a given text as a table. It includes columns for rowid, start position, end position, and the matched text. For performance, it's recommended to wrap the pattern with the regex() function, especially when the same pattern is used multiple times.
```sql
-- Find all 13-letter words
select rowid, start, end, match
from regex_find_all(
'\b\w{13}\b',
'Retroactively relinquishing remunerations is reprehensible.'
);
/*
┌───────┬───────┬─────┬───────────────┐
│ rowid │ start │ end │ match │
├───────┼───────┼─────┼───────────────┤
│ 0 │ 0 │ 13 │ Retroactively │
│ 1 │ 14 │ 27 │ relinquishing │
│ 2 │ 28 │ 41 │ remunerations │
│ 3 │ 45 │ 58 │ reprehensible │
└───────┴───────┴─────┴───────────────┘
*/
-- Extract all URLs from text (with cached regex for performance)
select match as url
from regex_find_all(
regex('https?://[^\s<>"{}|\\^`\[\]]+'),
'Visit https://example.com and http://test.org for more info'
);
/*
┌─────────────────────┐
│ url │
├─────────────────────┤
│ https://example.com │
│ http://test.org │
└─────────────────────┘
*/
-- Count matches per document
select
doc_id,
count(*) as email_count
from documents, regex_find_all(
regex('[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}') ,
documents.content
)
group by doc_id;
```
--------------------------------
### Load and Use sqlite-regex Extension
Source: https://github.com/asg017/sqlite-regex/blob/main/README.md
Demonstrates how to load the sqlite-regex extension and perform a basic regex match.
```sql
.load ./regex0
select 'foo' regexp 'f';
```
--------------------------------
### Deno Run Command with Permissions
Source: https://github.com/asg017/sqlite-regex/blob/main/deno/README.md
This command shows how to execute a Deno script that utilizes the x/sqlite_regex module. The '-A' flag grants all permissions (network, filesystem, FFI, etc.), and '--unstable' is required for certain Deno features used by the sqlite3 and sqlite_regex modules. This is the recommended way to run applications using these modules.
```bash
deno run -A --unstable
```
--------------------------------
### Load sqlite-regex with better-sqlite3
Source: https://github.com/asg017/sqlite-regex/blob/main/npm/sqlite-regex/README.md
Demonstrates how to load the sqlite-regex extension into a better-sqlite3 database instance and query the regex version. It uses `getLoadablePath()` to find the extension and `loadExtension()` to load it.
```javascript
import Database from "better-sqlite3";
import * as sqlite_regex from "sqlite-regex";
const db = new Database(":memory:");
db.loadExtension(sqlite_regex.getLoadablePath());
const version = db.prepare("select regex_version()").pluck().get();
console.log(version); // "v0.2.0"
```
--------------------------------
### Load sqlite-regex as a Runtime Extension
Source: https://github.com/asg017/sqlite-regex/blob/main/README.md
Demonstrates how to load the sqlite-regex library as a runtime extension in various environments. This method is useful for directly integrating regex capabilities into SQLite.
```sql
.load ./regex0
select regex_version();
-- v0.1.0
```
```python
import sqlite3
con = sqlite3.connect(":memory:")
con.enable_load_extension(True)
con.load_extension("./regex0")
print(con.execute("select regex_version()").fetchone())
# ('v0.1.0',)
```
```javascript
const Database = require("better-sqlite3");
const db = new Database(":memory:");
db.loadExtension("./regex0");
console.log(db.prepare("select regex_version()").get());
// { 'regex_version()': 'v0.1.0' }
```
```bash
datasette data.db --load-extension ./regex0
```
--------------------------------
### Load sqlite-regex Extension in SQLite CLI
Source: https://context7.com/asg017/sqlite-regex/llms.txt
Demonstrates how to load the sqlite-regex extension using the `.load` command in the SQLite command-line interface and verifies the version.
```sql
.load ./regex0
select regex_version();
-- 'v0.2.4-alpha.1'
```
--------------------------------
### Load sqlite-regex with node-sqlite3
Source: https://github.com/asg017/sqlite-regex/blob/main/npm/sqlite-regex/README.md
Shows how to load the sqlite-regex extension into a node-sqlite3 database instance and retrieve the regex version. It utilizes `getLoadablePath()` for the extension path and `loadExtension()` for loading.
```javascript
import sqlite3 from "sqlite3";
import * as sqlite_regex from "sqlite-regex";
const db = new sqlite3.Database(":memory:");
db.loadExtension(sqlite_regex.getLoadablePath());
db.get("select regex_version()", (err, row) => {
console.log(row); // {json_schema_version(): "v0.2.0"}
});
```
--------------------------------
### Use RegexSets for Efficient Multi-Pattern Matching in sqlite-regex
Source: https://github.com/asg017/sqlite-regex/blob/main/README.md
Demonstrates the use of `regexset` and `regexset_is_match` for efficiently checking if a string matches any pattern within a set of regular expressions.
```sql
select regexset_is_match(
regexset(
"bar",
"foo",
"barfoo"
),
'foobar'
)
```
--------------------------------
### Manually Load sqlite-regex Extension (Python)
Source: https://github.com/asg017/sqlite-regex/blob/main/python/sqlite_regex/README.md
Shows the manual process of loading the sqlite-regex extension by first enabling extension loading, then using `sqlite3.Connection.load_extension()` with the path obtained from `sqlite_regex.loadable_path()`, and finally disabling extension loading.
```python
import sqlite_regex
import sqlite3
conn = sqlite3.connect(':memory:')
conn.enable_load_extension(True)
loadable_ext_path = sqlite_regex.loadable_path()
conn.load_extension(loadable_ext_path)
conn.enable_load_extension(False)
result = conn.execute('select regex_version(), regex()').fetchone()
print(result)
# Expected output: ('v0.1.0', '01gr7gwc5aq22ycea6j8kxq4s9')
```
--------------------------------
### Determine Supported Platform
Source: https://github.com/asg017/sqlite-regex/blob/main/npm/sqlite-regex/README.md
Checks the current Node.js environment's platform and architecture to determine compatibility with the sqlite-regex package. This is useful for verifying if the pre-compiled extensions will be compatible.
```bash
$ node -e 'console.log([process.platform, process.arch])'
[ 'darwin', 'x64' ]
```
--------------------------------
### Load SQLite Regex Extension in Deno
Source: https://github.com/asg017/sqlite-regex/blob/main/deno/README.md
This snippet demonstrates how to import and load the sqlite-regex extension within a Deno application using the x/sqlite3 module. It initializes an in-memory database, enables extension loading, loads the sqlite_regex extension using its path, and then queries for the extension's version. This requires network and filesystem permissions.
```javascript
import { Database } from "https://deno.land/x/sqlite3@0.8.0/mod.ts";
import * as sqlite_regex from "https://deno.land/x/sqlite_regex@v0.2.4-alpha.1/mod.ts";
const db = new Database(":memory:");
db.enableLoadExtension = true;
db.loadExtension(sqlite_regex.getLoadablePath());
const [version] = db
.prepare("select regex_version()")
.value<[string]>()!;
console.log(version);
```
--------------------------------
### Load sqlite-regex Extension in Node.js
Source: https://context7.com/asg017/sqlite-regex/llms.txt
Illustrates loading the sqlite-regex extension in Node.js using the `better-sqlite3` library and the `sqlite-regex` package. It loads the extension and retrieves the version.
```javascript
import Database from "better-sqlite3";
import { getLoadablePath } from "sqlite-regex";
const db = new Database(":memory:");
db.loadExtension(getLoadablePath());
const version = db.prepare("select regex_version()").pluck().get();
console.log(version);
// 'v0.2.4-alpha.1'
```
--------------------------------
### Load sqlite-regex Extension in Python
Source: https://context7.com/asg017/sqlite-regex/llms.txt
Shows how to load the sqlite-regex extension within a Python script using the `sqlite3` module and the `sqlite_regex` package. It enables loadable extensions and verifies the version.
```python
import sqlite3
import sqlite_regex
# Using the Python package
conn = sqlite3.connect(":memory:")
conn.enable_load_extension(True)
sqlite_regex.load(conn)
# Verify it's loaded
result = conn.execute("select regex_version()").fetchone()
print(result)
# ('v0.2.4-alpha.1',)
```
--------------------------------
### Create RegexSet with regexset()
Source: https://context7.com/asg017/sqlite-regex/llms.txt
The regexset() function creates a RegexSet object, which allows for efficient matching of multiple regular expression patterns simultaneously. This is significantly faster than executing individual regex matches in sequence. The function takes multiple pattern strings as arguments.
```sql
-- Create a regexset (appears NULL but is valid)
select regexset(
'bar',
'foo',
'barfoo'
);
-- NULL (but is a regexset object)
-- Debug/view patterns
select regexset_print(regexset('abc', 'xyz', '\d+'));
-- '["abc","xyz","\\d+"]'
-- Invalid pattern in set throws error
select regexset('valid', '[invalid');
-- Error: regex parse error...
```
--------------------------------
### Compile SQLite Regex Extensions (C)
Source: https://github.com/asg017/sqlite-regex/blob/main/benchmarks/README.md
These commands compile C source files into shared libraries for SQLite regex extensions. They use GCC with optimization flags (-O3) and position-independent code (-fPIC). The first command compiles a generic regexp.c, while the second compiles sqlean/re, requiring additional source files and include paths.
```c
gcc -O3 -shared -fPIC regexp.c -o regexp.dylib
```
```c
gcc -O3 -shared -fPIC -I./ re.c sqlite3-re.c -o re.dylib
```
--------------------------------
### Create Compiled Regex Object with regex()
Source: https://context7.com/asg017/sqlite-regex/llms.txt
The `regex()` function creates a compiled regular expression object for efficient use in queries. While it appears as NULL in SQLite, it's a valid object. Invalid patterns will raise an error.
```sql
-- Create a regex object (appears NULL but is valid)
select regex('[0-9]{3}-[0-9]{3}-[0-9]{4}');
-- NULL (but is a regex object)
-- Invalid pattern throws error
select regex("[abc");
-- Error: Error parsing pattern as regex: ...
-- Use regex_print() to debug/view the pattern
select regex_print(regex('[abc]'));
-- '[abc]'
-- Use with table functions for better performance
select * from regex_find_all(
regex('\b\w+@\w+\.\w+\b'),
'Contact us at support@example.com or sales@company.org'
);
```
--------------------------------
### Create a regexset object
Source: https://github.com/asg017/sqlite-regex/blob/main/docs.md
Creates a regexset object from a list of patterns. This object is used with `regexset_is_match` and `regexset_matches`. The return value appears as NULL due to SQLite's pointer passing interface. Use `regexset_print` for debugging.
```sql
select regexset("bar", "foo", "barfoo");
-- NULL, but is still a regexset "object"
select regexset("[abc"); --errors
select regexset_print(regexset('abc', 'xyz')); -- '["abc","xyz"]'
```
--------------------------------
### Find First Regex Match with regex_find()
Source: https://context7.com/asg017/sqlite-regex/llms.txt
The `regex_find()` function locates and returns the first occurrence of a pattern within a text string. It returns NULL if no match is found and will error if the provided pattern is invalid.
```sql
-- Find a phone number
select regex_find(
'[0-9]{3}-[0-9]{3}-[0-9]{4}',
'Call us at 111-222-3333 or 444-555-6666'
);
-- '111-222-3333'
-- Find an email address
select regex_find(
'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}',
'Contact: john.doe@example.com for more info'
);
-- 'john.doe@example.com'
-- No match returns NULL
select regex_find('[0-9]+', 'no numbers here');
-- NULL
-- Extract from table data
select
id,
content,
regex_find('\$[0-9,]+(\.[0-9]{2})?', content) as price
from products;
```
--------------------------------
### Split String on Pattern Delimiter with sqlite-regex
Source: https://github.com/asg017/sqlite-regex/blob/main/README.md
Shows how to split a string into rows based on a regular expression delimiter using `regex_split`. The output includes the row ID and the split item.
```sql
select rowid, *
from regex_split('[ \t]+', 'a b c d e');
/*
┌───────┬──────┐
│ rowid │ item │
├───────┼──────┤
│ 0 │ a │
│ 1 │ b │
│ 2 │ c │
│ 3 │ d │
│ 4 │ e │
└───────┴──────┘
*/
```
--------------------------------
### regex_print(regex)
Source: https://github.com/asg017/sqlite-regex/blob/main/docs.md
Prints the pattern of a regex object created with `regex()`.
```APIDOC
## regex_print(regex)
### Description
Prints the pattern of a regex object created with `regex()`.
### Method
SQL Function
### Endpoint
N/A (SQL Function)
### Parameters
#### Path Parameters
- **regex** (object) - Required - The regex object created by `regex()`.
### Request Example
```sql
select regex_print(regex('[abc]'));
```
### Response
#### Success Response (String)
- **pattern** (string) - The regex pattern string.
#### Response Example
```sql
select regex_print(regex('[abc]')); -- '[abc]'
```
```
--------------------------------
### regex_find_all(pattern, text)
Source: https://github.com/asg017/sqlite-regex/blob/main/docs.md
Finds all instances of a pattern in the given text and returns a table of results.
```APIDOC
## regex_find_all(pattern, text)
### Description
Find all instances of a pattern in the given text. Returns a table with the matches.
### Method
SQL Function
### Endpoint
N/A (SQL Function)
### Parameters
#### Path Parameters
- **pattern** (string) - Required - The regular expression pattern.
- **text** (string) - Required - The text to search within.
### Request Example
```sql
select rowid, * from regex_find_all(regex('\\b\\w{13}\\b'), 'Retroactively relinquishing remunerations is reprehensible.');
```
### Response
#### Success Response (Table)
- **rowid** (integer) - The 0-based index of the match.
- **start** (integer) - The 0-based index of the starting character of the match inside the text.
- **end** (integer) - The 0-based index of the ending character of the match inside the text.
- **match** (string) - The full string match.
#### Response Example
```sql
select rowid, * from regex_find_all(regex('\\b\\w{13}\\b'), 'Retroactively relinquishing remunerations is reprehensible.');
/*
┌───────┬───────┬─────┬───────────────┐
│ rowid │ start │ end │ match │
├───────┼───────┼─────┼───────────────┤
│ 0 │ 0 │ 13 │ Retroactively │
│ 1 │ 14 │ 27 │ relinquishing │
│ 2 │ 28 │ 41 │ remunerations │
│ 3 │ 45 │ 58 │ reprehensible │
└───────┴───────┴─────┴───────────────┘
*/
```
```
--------------------------------
### Check for Any Pattern Match with regexset_is_match()
Source: https://context7.com/asg017/sqlite-regex/llms.txt
The regexset_is_match() function checks if any of the patterns within a given RegexSet object match the provided text. It returns 1 if at least one pattern matches, and 0 otherwise. This function is highly efficient for filtering or categorizing data based on multiple criteria.
```sql
-- Check if any pattern matches
select regexset_is_match(
regexset(
'bar',
'foo',
'barfoo'
),
'foobar'
);
-- 1
select regexset_is_match(
regexset(
'bar',
'foo',
'barfoo'
),
'xxx'
);
-- 0
-- Practical example: content filtering
select
id,
content,
case
when regexset_is_match(
regexset('spam', 'viagra', 'lottery', 'prince'),
lower(content)
) = 1 then 'SPAM'
else 'OK'
end as status
from messages;
```
--------------------------------
### Find all matching regex patterns in text
Source: https://github.com/asg017/sqlite-regex/blob/main/docs.md
Returns rows for each pattern in the regexset that matches the provided text. This function is based on Rust's `RegexSet.matches()`. It identifies which patterns matched, not the specific occurrences within the text.
```sql
select
key,
pattern
from regexset_matches(
regexset(
'\w+',
'\d+',
'\pL+',
'foo',
'bar',
'barfoo',
'foobar'
),
'foobar'
);
/*
┌─────┬─────────┐
│ key │ pattern │
├─────┼─────────┤
│ 0 │ \w+ │
│ 2 │ \pL+ │
│ 3 │ foo │
│ 4 │ bar │
│ 6 │ foobar │
└─────┴─────────┘
*/
```
--------------------------------
### Print patterns of a regexset object
Source: https://github.com/asg017/sqlite-regex/blob/main/docs.md
Prints the string representation of the patterns contained within a regexset object, which is created using the `regexset()` function.
```sql
select regexset_print(regexset('abc', 'xyz')); -- '["abc","xyz"]'
```
--------------------------------
### Create Regex Object with Caching
Source: https://github.com/asg017/sqlite-regex/blob/main/docs.md
Creates a regex 'object' from a pattern string using SQLite's pointer passing interface. This is useful for caching regex patterns in performance-critical queries involving table functions like regex_split() or regex_find_all(). The actual regex object is not directly visible and appears as NULL.
```sql
select regex('[abc]'); -- NULL, but is still a regex "object"
select regex("[abc"); -- Errors with 'Error parsing pattern as regex: ...'
```
--------------------------------
### Split text using regex pattern
Source: https://github.com/asg017/sqlite-regex/blob/main/docs.md
Splits the given text on each instance of the provided regex pattern. This function is based on Rust's `Regex.split()`. For performance, consider caching patterns using the `regex()` function.
```sql
select rowid, * from regex_split(regex('[ \t]+'), 'a b c d e');
/*
┌───────┬──────┐
│ rowid │ item │
├───────┼──────┤
│ 0 │ a │
│ 1 │ b │
│ 2 │ c │
│ 3 │ d │
│ 4 │ e │
└───────┴──────┘
*/
```
--------------------------------
### regex(pattern)
Source: https://github.com/asg017/sqlite-regex/blob/main/docs.md
Creates a regex "object" for caching regex patterns, useful for performance in heavy queries.
```APIDOC
## regex(pattern)
### Description
Creates a regex "object" with the given pattern. Useful for caching regex patterns in heavy queries.
### Method
SQL Function
### Endpoint
N/A (SQL Function)
### Parameters
#### Path Parameters
- **pattern** (string) - Required - The regular expression pattern.
### Request Example
```sql
select regex('[abc]');
```
### Response
#### Success Response (Object)
- **regex_object** (object) - A regex object. Note: will appear as NULL in SQLite.
#### Response Example
```sql
select regex('[abc]'); -- NULL, but is still a regex "object"
```
```
--------------------------------
### SQLite REGEXP Operator for Pattern Matching
Source: https://context7.com/asg017/sqlite-regex/llms.txt
Implements SQLite's REGEXP operator using the `regexp()` function. It returns 1 for a match and 0 otherwise, supporting both function syntax and the `X REGEXP Y` operator syntax for pattern matching.
```sql
-- Using regexp() function syntax
select regexp('[abc]', 'a');
-- 1
select regexp('[abc]', 'x');
-- 0
-- Using REGEXP operator syntax (X REGEXP Y calls regexp(Y, X))
select 'hello' regexp 'h.*o';
-- 1
select 'world' regexp '^hello';
-- 0
-- Practical example: filter rows matching a pattern
select * from users where email regexp '^[a-z]+@example\.com$';
```
--------------------------------
### regex_capture(pattern, text, group)
Source: https://github.com/asg017/sqlite-regex/blob/main/docs.md
Returns the text of the capture group with the specific group index or name.
```APIDOC
## regex_capture(pattern, text, group)
### Description
Returns the text of the capture group with the specific `group` index or name, or NULL otherwise.
### Method
SQL Function
### Endpoint
N/A (SQL Function)
### Parameters
#### Path Parameters
- **pattern** (string) - Required - The regular expression pattern.
- **text** (string) - Required - The text to search within.
- **group** (integer/string) - Required - The capture group index (0 for the entire match) or name.
### Request Example
```sql
select regex_capture(
"'(?P[^']+)'\\s+\\((?P\\d{4})\\)",
"Not my favorite movie: 'Citizen Kane' (1941).",
1
);
```
### Response
#### Success Response (String)
- **capture** (string) - The captured text, or NULL if the group is not found.
#### Response Example
```sql
select regex_capture(
"'(?P[^']+)'\\s+\\((?P\\d{4})\\)",
"Not my favorite movie: 'Citizen Kane' (1941).",
1
);
-- "Citizen Kane"
```
```
--------------------------------
### Print Regex Object Pattern
Source: https://github.com/asg017/sqlite-regex/blob/main/docs.md
Prints the pattern string of a regex object that was created using the regex() function. This is helpful for debugging and verifying the pattern stored within a regex object.
```sql
select regex_print(regex('[abc]')); -- '[abc]'
```
--------------------------------
### Replace Occurrences of a Pattern with Another String in sqlite-regex
Source: https://github.com/asg017/sqlite-regex/blob/main/README.md
Illustrates how to replace occurrences of a regular expression pattern within a string using `regex_replace` and `regex_replace_all`. Supports backreferences for replacement.
```sql
select regex_replace(
'(?P[^,\s]+),\s+(?P\S+)',
'Springsteen, Bruce',
'$first $last'
);
-- 'Bruce Springsteen'
select regex_replace_all('a', 'abc abc', '');
-- 'bc bc'
```
--------------------------------
### REGEXP Operator
Source: https://github.com/asg017/sqlite-regex/blob/main/docs.md
Implements the REGEXP operator for SQLite, allowing regular expression matching in SQL queries.
```APIDOC
## REGEXP Operator
### Description
Allows regular expression matching using the `text regexp pattern` or `regexp(pattern, text)` syntax.
### Method
N/A (SQL Operator)
### Endpoint
N/A (SQL Operator)
### Parameters
#### Path Parameters
- **text** (string) - Required - The text to search within.
- **pattern** (string) - Required - The regular expression pattern.
### Request Example
```sql
select regexp('[abc]', 'a'); -- 1
```
### Response
#### Success Response (Boolean)
- **result** (integer) - 1 if the pattern matches, 0 otherwise.
#### Response Example
```sql
select regexp('[abc]', 'a'); -- 1
```
```
--------------------------------
### Check if text matches any regex in a set
Source: https://github.com/asg017/sqlite-regex/blob/main/docs.md
Returns 1 if the given text matches any of the patterns within the provided regexset. This function is based on Rust's `RegexSet.is_match()`.
```sql
select regexset_is_match(
regexset(
"bar",
"foo",
"barfoo"
),
'foobar'
); -- 1
select regexset_is_match(
regexset(
"bar",
"foo",
"barfoo"
),
'xxx'
); -- 0
```
--------------------------------
### Replace First Regex Match with regex_replace()
Source: https://context7.com/asg017/sqlite-regex/llms.txt
The regex_replace() function replaces the first occurrence of a regular expression pattern within a string with a specified replacement text. It supports backreferences to capture groups using $1, $2, or $name syntax, allowing for complex text manipulations and reformatting.
```sql
-- Simple replacement
select regex_replace(
'[^01]+',
'1078910',
''
);
-- '1010'
-- Swap name order using capture groups
select regex_replace(
'(?P[^,\s]+),\s+(?P\S+)',
'Springsteen, Bruce',
'$first $last'
);
-- 'Bruce Springsteen'
-- Format phone number
select regex_replace(
'(\d{3})(\d{3})(\d{4})',
'1112223333',
'($1) $2-$3'
);
-- '(111) 222-3333'
-- Redact sensitive data (first occurrence only)
select regex_replace(
'\d{3}-\d{2}-\d{4}',
'SSN: 123-45-6789, another: 987-65-4321',
'XXX-XX-XXXX'
);
-- 'SSN: XXX-XX-XXXX, another: 987-65-4321'
```
--------------------------------
### SQLite REGEXP Operator Implementation
Source: https://github.com/asg017/sqlite-regex/blob/main/docs.md
Implements the REGEXP operator for SQLite, allowing regex matching directly in SQL queries. It can be used with 'text regexp pattern' or 'regexp(pattern, text)' syntax. The underlying regex pattern syntax is defined by the 'regex' crate.
```sql
select regexp('[abc]', 'a'); -- 1
select regexp('[abc]', 'x'); -- 0
select 'a' regexp '[abc]'; -- 1
select 'x' regexp '[abc]'; -- 0
```
--------------------------------
### Replace All Matches with regex_replace_all()
Source: https://context7.com/asg017/sqlite-regex/llms.txt
The regex_replace_all() function replaces all occurrences of a specified pattern within a string with a given replacement text. It supports the same replacement string syntax as regex_replace(). This function is useful for tasks like data normalization, redaction, or simple text substitution.
```sql
-- Replace all occurrences
select regex_replace_all(
'dog',
'cat dog mouse dog',
'monkey'
);
-- 'cat monkey mouse monkey'
-- Remove all vowels
select regex_replace_all(
'[aeiou]',
'hello world',
''
);
-- 'hll wrld'
-- Normalize whitespace
select regex_replace_all(
'\s+',
'too many spaces here',
' '
);
-- 'too many spaces here'
-- Redact all sensitive data
select regex_replace_all(
'\d{3}-\d{2}-\d{4}',
'SSN: 123-45-6789, backup: 987-65-4321',
'XXX-XX-XXXX'
);
-- 'SSN: XXX-XX-XXXX, backup: XXX-XX-XXXX'
```
--------------------------------
### regex_find(pattern, text)
Source: https://github.com/asg017/sqlite-regex/blob/main/docs.md
Finds and returns the text of the given pattern in the string.
```APIDOC
## regex_find(pattern, text)
### Description
Find and return the text of the given pattern in the string, or NULL otherwise. Errors if `pattern` is not legal regex.
### Method
SQL Function
### Endpoint
N/A (SQL Function)
### Parameters
#### Path Parameters
- **pattern** (string) - Required - The regular expression pattern.
- **text** (string) - Required - The text to search within.
### Request Example
```sql
select regex_find('[0-9]{3}-[0-9]{3}-[0-9]{4}', 'phone: 111-222-3333');
```
### Response
#### Success Response (String)
- **match** (string) - The matched text, or NULL if no match.
#### Response Example
```sql
select regex_find('[0-9]{3}-[0-9]{3}-[0-9]{4}', 'phone: 111-222-3333');
-- '111-222-3333'
```
```
--------------------------------
### regex_replace_all
Source: https://github.com/asg017/sqlite-regex/blob/main/docs.md
Replaces all instances of a pattern within a text with a specified replacement string. Supports the regex replacement string syntax.
```APIDOC
## POST /regex/replace_all
### Description
Replace **all** instances of `pattern` inside `text` with the given `replacement` text. Supports the [replacment string syntax](https://docs.rs/regex/latest/regex/struct.Regex.html#replacement-string-syntax).
### Method
POST
### Endpoint
/regex/replace_all
### Parameters
#### Request Body
- **pattern** (string) - Required - The regular expression pattern to find.
- **text** (string) - Required - The text to perform the replacements on.
- **replacement** (string) - Required - The string to replace the matched patterns with.
### Request Example
```json
{
"pattern": "dog",
"text": "The dog chased the other dog.",
"replacement": "cat"
}
```
### Response
#### Success Response (200)
- **result** (string) - The text with all occurrences of the pattern replaced.
#### Response Example
```json
{
"result": "The cat chased the other cat."
}
```
```
--------------------------------
### Extract Capture Groups by Index using regex_captures and regex_capture (SQL)
Source: https://github.com/asg017/sqlite-regex/blob/main/docs.md
This SQL query utilizes `regex_captures` to iterate through all matches of a pattern and `regex_capture` to extract capture groups by their numerical index (0 for the entire match, 1 for the first group, etc.). It shows how to access both the full match and individual capture groups.
```sql
select
rowid,
captures,
regex_capture(captures, 0) as "0",
regex_capture(captures, 1) as "1",
regex_capture(captures, 2) as "2",
regex_capture(captures, 3) as "3"
from regex_captures(
regex("'\(?P[^']+'\)\s+\((?P\d{4})\)"),
"'Citizen Kane' (1941), 'The Wizard of Oz' (1939), 'M' (1931)."
);
```
--------------------------------
### Find First Regex Match in Text
Source: https://github.com/asg017/sqlite-regex/blob/main/docs.md
Finds the first occurrence of a regex pattern within a given text string. It returns the matched text or NULL if no match is found. The function will error if the provided pattern is not a legal regular expression. This function is based on the `Regex.find()` method.
```sql
select regex_find(
'[0-9]{3}-[0-9]{3}-[0-9]{4}',
'phone: 111-222-3333'
);
-- '111-222-3333'
```
--------------------------------
### regex_replace
Source: https://github.com/asg017/sqlite-regex/blob/main/docs.md
Replaces the first instance of a pattern within a text with a specified replacement string. Supports the regex replacement string syntax.
```APIDOC
## POST /regex/replace
### Description
Replace the **first** instance of `pattern` inside `text` with the given `replacement` text. Supports the [replacment string syntax](https://docs.rs/regex/latest/regex/struct.Regex.html#replacement-string-syntax).
### Method
POST
### Endpoint
/regex/replace
### Parameters
#### Request Body
- **pattern** (string) - Required - The regular expression pattern to find.
- **text** (string) - Required - The text to perform the replacement on.
- **replacement** (string) - Required - The string to replace the matched pattern with.
### Request Example
```json
{
"pattern": "(?P[^,\s]+),\s+(?P\S+)",
"text": "Springsteen, Bruce",
"replacement": "$first $last"
}
```
### Response
#### Success Response (200)
- **result** (string) - The text with the first occurrence of the pattern replaced.
#### Response Example
```json
{
"result": "Bruce Springsteen"
}
```
```
--------------------------------
### Extract Capture Group Values by Index or Name with sqlite-regex
Source: https://github.com/asg017/sqlite-regex/blob/main/README.md
Illustrates how to extract specific capture group values from a regex match using `regex_capture`. It supports both numerical indices and named capture groups.
```sql
select
regex_capture(captures, 0) as entire_match,
regex_capture(captures, 'title') as title,
regex_capture(captures, 'year') as year
from regex_captures(
regex("'(?P[^']+)''\s+\((?P\d{4})\)"),
"'Citizen Kane' (1941), 'The Wizard of Oz' (1939), 'M' (1931)."
);
/*
┌───────────────────────────┬──────────────────┬──────┐
│ entire_match │ title │ year │
├───────────────────────────┼──────────────────┼──────┤
│ 'Citizen Kane' (1941) │ Citizen Kane │ 1941 │
│ 'The Wizard of Oz' (1939) │ The Wizard of Oz │ 1939 │
│ 'M' (1931) │ M │ 1931 │
└───────────────────────────┴──────────────────┴──────┘
*/
```
--------------------------------
### Extract Regex Capture Groups with regex_capture()
Source: https://context7.com/asg017/sqlite-regex/llms.txt
The regex_capture() function extracts specific capture group values from a regular expression match. Capture groups can be referenced by their numerical index (0 for the entire match) or by their assigned name. If a specified group does not exist, it returns NULL. This function can be used independently or in conjunction with regex_captures().
```sql
-- Extract by index (0 = entire match, 1 = first group, etc.)
select regex_capture(
"'(?P[^']+)'\s+\((?P\d{4})\)",
"Not my favorite movie: 'Citizen Kane' (1941).",
0
);
-- "'Citizen Kane' (1941)"
select regex_capture(
"'(?P[^']+)'\s+\((?P\d{4})\)",
"Not my favorite movie: 'Citizen Kane' (1941).",
1
);
-- "Citizen Kane"
select regex_capture(
"'(?P[^']+)'\s+\((?P\d{4})\)",
"Not my favorite movie: 'Citizen Kane' (1941).",
2
);
-- "1941"
-- Extract by named group
select regex_capture(
"'(?P[^']+)'\s+\((?P\d{4})\)",
"Not my favorite movie: 'Citizen Kane' (1941).",
'title'
);
-- "Citizen Kane"
-- Non-existent group returns NULL
select regex_capture(
"'(?P[^']+)'\s+\((?P\d{4})\)",
"Not my favorite movie: 'Citizen Kane' (1941).",
'director'
);
-- NULL
```