Skip to main content
Log in

Mojo function

expand_character_class

expand_character_class(class_str: String) -> String

Expand a character class pattern to its member characters.

This function takes a regex character class specification and expands it into a string containing all the individual characters that are members of that class. This is useful for pattern matching operations that need to know exactly which characters are included in a class.

The function handles:

  1. Predefined character classes like \d, \w, \s and their negations.
  2. Custom character classes defined with brackets like [a-z0-9].
  3. Character ranges within custom classes like a-z (expands to all letters).
  4. Negated character classes with ^ like [^0-9] (all non-digits).

For negated character classes, the function returns all ASCII printable characters (codes 32-126) that are not in the specified class.

Example:

var digits = expand_character_class("\d")  # "0123456789".
var lowercase = expand_character_class("[a-z]") # "abcdefghijklmnopqrstuvwxyz".
var non_digits = expand_character_class("[^0-9]") # All printable non-digit chars.
var digits = expand_character_class("\d")  # "0123456789".
var lowercase = expand_character_class("[a-z]") # "abcdefghijklmnopqrstuvwxyz".
var non_digits = expand_character_class("[^0-9]") # All printable non-digit chars.

Args:

  • class_str (String): A character class pattern to expand. This can be either a predefined class like "\d" or a custom class like "[a-z0-9]".

Returns:

A string containing all the individual characters that are members of the specified character class.

Raises:

If string operations fail during processing (unlikely with normal usage).