String

This page documents a checker for string arguments.

class argscheck.string.String(*args, **kwargs)

Check if x is a string and optionally, if it matches a particular regex pattern.

Regex matching is done using the builtin re module.

Parameters
  • patternOptional[str] – If provided, x must match this regex pattern.

  • flagsOptional[re.RegexFlag] – Flags used for modifying the matching behaviour. Only relevant if pattern is provided.

  • methodstr – Name of re.Pattern method that will be used to match x against the regex pattern. Must be “match”, “fullmatch” or “search”. Only relevant if pattern is provided.

Example

from argscheck import check, String


# Check if a string ending with ".exe"
checker = String(".*\.exe$")

check(checker, "app.exe")    # Passes, "app.exe" is returned
check(checker, "script.sh")  # Fails, a ValueError is raised ("script.sh" does not end with ".exe")