Path: redb-route/redb.Route/Predicates/RegexPredicate.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Predicates/RegexPredicate.cs
----
using System.Text.RegularExpressions;
using redb.Route.Abstractions;
namespace redb.Route.Predicates;
///
/// Predicate that checks whether a string expression result matches a regular expression pattern.
///
public class RegexPredicate : IPredicate
{
private readonly IExpression _expression;
private readonly Regex _regex;
///
/// Initializes a new instance with the specified expression and regex pattern.
///
/// The expression to evaluate.
/// The regular expression pattern to match against.
public RegexPredicate(IExpression expression, string pattern)
{
_expression = expression;
_regex = new Regex(pattern);
}
///
public bool Matches(IExchange exchange)
{
var result = _expression.Evaluate(exchange);
return result != null && _regex.IsMatch(result);
}
///
public Task MatchesAsync(IExchange exchange)
=> Task.FromResult(Matches(exchange));
}