mirror of
https://github.com/aljazceru/turso.git
synced 2026-02-06 16:54:23 +01:00
add regexp_capture
This commit is contained in:
@@ -82,3 +82,36 @@ fn regexp_replace(&self, args: &[Value]) -> Value {
|
||||
_ => Value::from_text("".to_string()), // Return an empty string for invalid value types
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[scalar(name = "regexp_capture")]
|
||||
fn regexp_capture(args: &[Value]) -> Value {
|
||||
match (args.get(0), args.get(1)) {
|
||||
(Some(source), Some(pattern)) if source.value_type() == ValueType::Text && pattern.value_type() == ValueType::Text => {
|
||||
let Some(source_text) = source.to_text() else {
|
||||
return Value::null();
|
||||
};
|
||||
|
||||
let Some(pattern_text) = pattern.to_text() else {
|
||||
return Value::null();
|
||||
};
|
||||
|
||||
let group_index: usize = args.get(2).and_then(|v| v.to_integer()).map(|n| n as usize).unwrap_or(1);
|
||||
|
||||
|
||||
let re = match Regex::new(pattern_text) {
|
||||
Ok(re) => re,
|
||||
Err(_) => return Value::null()
|
||||
};
|
||||
|
||||
if let Some(caps) = re.captures(source_text) {
|
||||
if let Some(m) = caps.get(group_index) {
|
||||
return Value::from_text(m.as_str().to_string());
|
||||
}
|
||||
}
|
||||
|
||||
Value::null()
|
||||
}
|
||||
_ => Value::null()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user