matches static method

bool matches(
  1. String text,
  2. String pattern
)

Verifica si un texto coincide con un patrón que puede contener wildcards (*)

Examples:

  • matches("hello.world", "hello.*") returns true
  • matches("api.example.com", "*.example.*") returns true
  • matches("test", "test") returns true
  • matches("testing", "test") returns false

Implementation

static bool matches(String text, String pattern) {
  if (pattern.isEmpty) return text.isEmpty;
  if (text.isEmpty) return pattern == '*' || pattern.isEmpty;

  // Si no hay wildcards, usar comparación directa
  if (!pattern.contains('*')) {
    return text.toLowerCase().contains(pattern.toLowerCase());
  }

  // Convertir el patrón a regex escapando caracteres especiales excepto *
  final regexPattern = _convertWildcardToRegex(pattern);

  try {
    final regex = RegExp(regexPattern, caseSensitive: false);
    return regex.hasMatch(text);
  } catch (e) {
    // Si hay error en el regex, fallback a comparación simple
    return text.toLowerCase().contains(pattern.toLowerCase().replaceAll('*', ''));
  }
}