Implementation:Ggml org Llama cpp Regex Partial Header
| Knowledge Sources | |
|---|---|
| Domains | Parsing, Regex |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Declares the interface for regex matching with partial match support.
Description
Defines the `common_regex_match_type` enum (none, partial, full), `common_string_range` for match group positions with begin/end offsets, and `common_regex_match` for match results containing the type and a vector of groups. The `common_regex` class wraps a pattern string, a compiled `std::regex`, and a precomputed reversed partial regex. Exposes `search()` for full/partial matching and `regex_to_reversed_partial_regex()` as a public test helper.
Usage
Include this header to use the partial regex matching API in constrained generation systems. It provides the type definitions and class interface needed to detect both full and partial regex matches on streaming model output.
Code Reference
Source Location
- Repository: Ggml_org_Llama_cpp
- File: common/regex-partial.h
- Lines: 1-56
Signature
enum common_regex_match_type {
COMMON_REGEX_MATCH_TYPE_NONE,
COMMON_REGEX_MATCH_TYPE_PARTIAL,
COMMON_REGEX_MATCH_TYPE_FULL,
};
struct common_string_range {
size_t begin;
size_t end;
common_string_range(size_t begin, size_t end);
bool empty() const;
bool operator==(const common_string_range & other) const;
};
struct common_regex_match {
common_regex_match_type type = COMMON_REGEX_MATCH_TYPE_NONE;
std::vector<common_string_range> groups;
bool operator==(const common_regex_match & other) const;
bool operator!=(const common_regex_match & other) const;
};
class common_regex {
public:
explicit common_regex(const std::string & pattern);
common_regex_match search(const std::string & input, size_t pos, bool as_match = false) const;
const std::string & str() const;
};
std::string regex_to_reversed_partial_regex(const std::string & pattern);
Import
#include "regex-partial.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| pattern | const std::string & | Yes | Regex pattern to compile and use for matching |
| input | const std::string & | Yes | String to search for full or partial matches |
| pos | size_t | Yes | Starting position in the input string |
| as_match | bool | No | If true, requires the full remaining string to match (defaults to false) |
Outputs
| Name | Type | Description |
|---|---|---|
| match | common_regex_match | Result containing match type (NONE/PARTIAL/FULL) and group positions |
| str() | const std::string & | The original pattern string |
Usage Examples
#include "regex-partial.h"
// Construct a regex with partial match support
common_regex rx("\\d{3}-\\d{4}");
// Search for a match
common_regex_match result = rx.search("555-1234 extra", 0);
if (result.type == COMMON_REGEX_MATCH_TYPE_FULL) {
// result.groups[0] contains the full match range
auto range = result.groups[0];
// range.begin == 0, range.end == 8
}
// Check for partial match at end of streaming input
common_regex_match partial = rx.search("555-12", 0, true);
// partial.type == COMMON_REGEX_MATCH_TYPE_PARTIAL