Skip to content

Regular Expressions

A set of tools for working with regular expressions. Can recognize regular expressions over the alphabet of ascii letters (lower and upper case), ascii digits, and subsets of these.

A regular expression with the following operations only are supported in this library:

  • *: Kleene star operation, language repeated zero or more times. Ex: a*,(ab)*
  • +: Kleene plus operation, language repeated one or more times. Ex: a+,(ab)+
  • ?: Language repeated zero or one time. Ex: a?
  • Concatenation. Ex: abcd
  • |: Union. Ex: a|b
  • &: Intersection. Ex: a&b
  • .: Wildcard. Ex: a.b
  • ^: Shuffle. Ex: a^b
  • {}: Quantifiers expressing finite repetitions. Ex: a{1,2},a{3,}
  • (): The empty string.
  • (...): Grouping.

This is similar to the Python re module, but this library does not support any special characters other than those given above. All regular languages can be written with these.

isequal(re1, re2, *, input_symbols=None)

Whether both regular expressions are equivalent.

Parameters:

Name Type Description Default
re1 str

The first regular expression as a string.

required
re2 str

The second regular expression as a string.

required
input_symbols Optional[AbstractSet[str]]

The set of input symbols when doing the comparison. Defaults to all ascii letters and digits.

None

Returns:

Type Description
bool

Whether the regular expressions are equivalent.

issubset(re1, re2, *, input_symbols=None)

Whether re1 is a subset of re2.

Parameters:

Name Type Description Default
re1 str

The first regular expression as a string.

required
re2 str

The second regular expression as a string.

required
input_symbols Optional[AbstractSet[str]]

The set of input symbols when doing the comparison. Defaults to all ascii letters and digits.

None

Returns:

Type Description
bool

True if re1 is a subset of re2.

issuperset(re1, re2, *, input_symbols=None)

Whether re1 is a superset of re2.

Parameters:

Name Type Description Default
re1 str

The first regular expression as a string.

required
re2 str

The second regular expression as a string.

required
input_symbols Optional[AbstractSet[str]]

The set of input symbols when doing the comparison. Defaults to all ascii letters and digits.

None

Returns:

Type Description
bool

True if re1 is a superset of re2.

validate(regex)

Raises an exception if the input regular expression is invalid.

Raises:

Type Description
InvalidRegexError

Raised if the regex given as input is not well defined.