@sentrie/regex
The @sentrie/regex module provides regular expression pattern matching and manipulation utilities. All patterns are compiled and cached for performance.
use { match, find, findAll, replace, replaceAll, split } from @sentrie/regexFunctions
Section titled “Functions”match(pattern: string, str: string): boolean
Section titled “match(pattern: string, str: string): boolean”Tests if a string matches a regular expression pattern.
Parameters:
pattern- The regular expression pattern to matchstr- The string to test
Returns: true if the string matches the pattern, false otherwise
Throws: Error if the pattern is invalid
Example:
use { match } from @sentrie/regexlet matches = regex.match("^[a-z]+$", "hello") // truelet noMatch = regex.match("^[0-9]+$", "hello") // falsefind(pattern: string, str: string): string | null
Section titled “find(pattern: string, str: string): string | null”Finds the first match of a pattern in a string.
Parameters:
pattern- The regular expression pattern to search forstr- The string to search in
Returns: The first match found, or null if no match
Throws: Error if the pattern is invalid
Example:
use { find } from @sentrie/regexlet match = regex.find("[0-9]+", "abc123def") // "123"let noMatch = regex.find("[0-9]+", "abcdef") // nullfindAll(pattern: string, str: string): string[]
Section titled “findAll(pattern: string, str: string): string[]”Finds all matches of a pattern in a string.
Parameters:
pattern- The regular expression pattern to search forstr- The string to search in
Returns: Array of all matches found (empty array if none)
Throws: Error if the pattern is invalid
Example:
use { findAll } from @sentrie/regexlet matches = regex.findAll("[0-9]+", "abc123def456ghi") // ["123", "456"]replace(pattern: string, str: string, replacement: string): string
Section titled “replace(pattern: string, str: string, replacement: string): string”Replaces the first occurrence of a pattern in a string.
Parameters:
pattern- The regular expression pattern to matchstr- The string to perform replacement onreplacement- The replacement string
Returns: The string with the first match replaced
Throws: Error if the pattern is invalid
Example:
use { replace } from @sentrie/regexlet result = regex.replace("[0-9]+", "abc123def", "XXX") // "abcXXXdef"replaceAll(pattern: string, str: string, replacement: string): string
Section titled “replaceAll(pattern: string, str: string, replacement: string): string”Replaces all occurrences of a pattern in a string.
Parameters:
pattern- The regular expression pattern to matchstr- The string to perform replacement onreplacement- The replacement string
Returns: The string with all matches replaced
Throws: Error if the pattern is invalid
Example:
use { replaceAll } from @sentrie/regexlet result = regex.replaceAll("[0-9]+", "abc123def456", "XXX") // "abcXXXdefXXX"split(pattern: string, str: string): string[]
Section titled “split(pattern: string, str: string): string[]”Splits a string by a regular expression pattern.
Parameters:
pattern- The regular expression pattern to split onstr- The string to split
Returns: Array of substrings split by the pattern
Throws: Error if the pattern is invalid
Example:
use { split } from @sentrie/regexlet parts = regex.split("\\s+", "hello world test") // ["hello", "world", "test"]Complete Example
Section titled “Complete Example”namespace com/example/validation
policy mypolicy { use { match, find, replaceAll } from @sentrie/regex fact email!: string fact phoneNumber!: string
rule validateEmail = default false { let emailPattern = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$" yield regex.match(emailPattern, email) }
rule validatePhone = default false { let phonePattern = "^\\+?[1-9]\\d{1,14}$" yield regex.match(phonePattern, phoneNumber) }
rule sanitizeInput = default false { let sanitized = regex.replaceAll("[^a-zA-Z0-9]", email, "") yield sanitized != "" }
export decision of validateEmail export decision of validatePhone export decision of sanitizeInput}Common Patterns
Section titled “Common Patterns”Email Validation
Section titled “Email Validation”let emailPattern = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"Phone Number (E.164)
Section titled “Phone Number (E.164)”let phonePattern = "^\\+?[1-9]\\d{1,14}$"URL Pattern
Section titled “URL Pattern”let urlPattern = "^https?://[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}(/.*)?$"Alphanumeric with Underscores
Section titled “Alphanumeric with Underscores”let alnumPattern = "^[a-zA-Z0-9_]+$"Performance Notes
Section titled “Performance Notes”- All patterns are compiled and cached for performance
- Repeated use of the same pattern in a single execution context will use the cached compiled pattern
- Patterns are automatically cleaned up after execution