From 5b2db043fa1e6c3709fa0eb27f85be4f96db6f02 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sun, 8 Feb 2026 23:14:28 +0200 Subject: [PATCH] regex.pcre: add a backwards compatibility shim for examples, that use `vlang/pcre` (made using Gemini 3) (#26553) --- examples/regex/{pcre.vv => pcre.v} | 8 ++++---- vlib/regex/pcre/regex.v | 31 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) rename examples/regex/{pcre.vv => pcre.v} (85%) diff --git a/examples/regex/pcre.vv b/examples/regex/pcre.v similarity index 85% rename from examples/regex/pcre.vv rename to examples/regex/pcre.v index bbe33d738..a9ae29c23 100644 --- a/examples/regex/pcre.vv +++ b/examples/regex/pcre.v @@ -1,8 +1,8 @@ -module main +import regex.pcre -// NB: you need to `v install pcre` to be able to compile this example. - -import pcre +// Since 2026/02/08, regex.pcre is built-in and implemented in pure V. +// The same example will compile and run with the C wrapper of the PCRE library +// too, but you will need to `import pcre` instead. fn example() { r := pcre.new_regex('Match everything after this: (.+)', 0) or { diff --git a/vlib/regex/pcre/regex.v b/vlib/regex/pcre/regex.v index 8856b957f..f153ab521 100644 --- a/vlib/regex/pcre/regex.v +++ b/vlib/regex/pcre/regex.v @@ -112,6 +112,25 @@ pub: groups []string } +// get retrieves the captured text by index. +// Index 0 returns the whole match, 1+ returns capture groups. +pub fn (m Match) get(idx int) ?string { + if idx == 0 { + return m.text + } + if idx > 0 && idx <= m.groups.len { + return m.groups[idx - 1] + } + return none +} + +// get_all returns the whole match followed by all capture groups. +pub fn (m Match) get_all() []string { + mut res := [m.text] + res << m.groups + return res +} + // --- AST Nodes --- // Quantifier represents a repetition range. @@ -261,6 +280,13 @@ pub fn compile(pattern string) !Regex { } } +// new_regex is an alias for compile, for compatibility with older PCRE wrappers. +// Note: The second argument (flags) is currently ignored as flags should be +// embedded in the pattern (e.g., '(?i)pattern'). +pub fn new_regex(pattern string, _ int) !Regex { + return compile(pattern) +} + // Compiler manages the state of the bytecode generation process. struct Compiler { mut: @@ -1158,3 +1184,8 @@ pub fn (r Regex) find_from(text string, start_index int) ?Match { } return none } + +// match_str is an alias for find_from, for compatibility with older PCRE wrappers. +pub fn (r Regex) match_str(text string, start_index int, _ int) ?Match { + return r.find_from(text, start_index) +} -- 2.39.5