CQL provides powerful methods for filtering and counting groups based on name patterns using the groups().filter() method. This article explains how to use pattern matching with the matchesIn() function to perform common group-related operations.
To count the number of groups that match a specific pattern, use the length property on the filtered groups collection:
groups().filter((group) => group.name.matchesIn("pattern")).lengthFor example, to count groups with names matching "tier:0", "tier:1", or "tier:2":
groups().filter((group) => group.name.matchesIn("tier:[0-2]")).lengthThis query returns the total number of groups that match the specified pattern.
To check if an entity has more than one group matching a pattern, compare the length of filtered results to 1:
groups().filter((group) => group.name.matchesIn("pattern")).length > 1This expression returns true if there are multiple groups matching the pattern, and false otherwise.
To validate that an entity has exactly one group matching a specific pattern:
groups().filter((group) => group.name.matchesIn("pattern")).length == 1This expression returns true only if exactly one group matches the specified pattern, and false if there are zero or more than one matching groups.