Skip to content

Commit 9bf8d80

Browse files
committed
Add 'Regex Match' node
1 parent c955d2f commit 9bf8d80

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

node-graph/nodes/gcore/src/logic.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,37 @@ async fn switch<T, C: Send + 'n + Clone>(
587587
if condition { if_true.eval(ctx).await } else { if_false.eval(ctx).await }
588588
}
589589

590+
/// Tests whether a regular expression pattern matches within the string, returning true or false.
591+
#[node_macro::node(category("Text"))]
592+
fn regex_match(
593+
_: impl Ctx,
594+
/// The string to test against.
595+
string: String,
596+
/// The regular expression pattern to match.
597+
pattern: String,
598+
/// Require the pattern to match the entire string, not just any portion.
599+
entire_string: bool,
600+
/// Match letters regardless of case.
601+
case_insensitive: bool,
602+
/// Make `^` and `$` match the start and end of each line, not just the whole string.
603+
multiline: bool,
604+
) -> bool {
605+
let flags = match (case_insensitive, multiline) {
606+
(false, false) => "",
607+
(true, false) => "(?i)",
608+
(false, true) => "(?m)",
609+
(true, true) => "(?im)",
610+
};
611+
let wrapped_pattern = if entire_string { format!("{flags}\\A(?:{pattern})\\z") } else { format!("{flags}{pattern}") };
612+
613+
let Ok(regex) = fancy_regex::Regex::new(&wrapped_pattern) else {
614+
log::error!("Invalid regex pattern: {pattern}");
615+
return false;
616+
};
617+
618+
regex.is_match(&string).unwrap_or(false)
619+
}
620+
590621
/// Replaces matches of a regular expression pattern in the string. The replacement string supports backreferences: `$0` for the whole match, `$1`, `$2`, etc. for capture groups.
591622
#[node_macro::node(category("Text"))]
592623
fn regex_replace(

0 commit comments

Comments
 (0)