pregex.core.assertions

All classes within this module “assert” something about the provided pattern without having to match any additional characters. For example, MatchAtStart ensures that the provided pattern matches only when it is found at the start of the string, while NotFollowedBy asserts that a match must not be followed by one or more specified patterns. Another thing you should keep in mind is that many of these assertions cannot be repeated, as attempting that will cause a CannotBeRepeatedException exception to be thrown.

Classes & methods

Below are listed all classes within pregex.core.assertions along with any possible methods they may possess.

class pregex.core.assertions.EnclosedBy(match: Union[Pregex, str], *assertions: Union[Pregex, str])[source]

Matches pattern match only if it is both directly preceded and followed by all of the provided assertion patterns.

Parameters
  • match (Pregex | str) – A Pregex instance or string representing the match pattern.

  • *assertions (Pregex | str) – One or more patterns, all of which must come both right before and right after pattern match in order for it to be considered a match.

Raises
  • NotEnoughArgumentsException – No assertion patterns were provided.

  • InvalidArgumentTypeException – At least one of the provided arguments is neither a Pregex instance nor a string.

  • NonFixedWidthPatternException – Parameter assertion corresponds to a pattern that does not have a fixed width.

Note

The resulting pattern cannot have a repeating quantifier applied to it.

class pregex.core.assertions.FollowedBy(match: Union[Pregex, str], *assertions: Union[Pregex, str])[source]

Matches pattern match only if it is directly followed by all of the provided assertion patterns.

Parameters
  • match (Pregex | str) – A Pregex instance or string representing the match pattern.

  • *assertions (Pregex | str) – One or more patterns, all of which must come right after pattern match in order for it to be considered a match.

Raises
  • NotEnoughArgumentsException – No assertion patterns were provided.

  • InvalidArgumentTypeException – At least one of the provided arguments is neither a Pregex instance nor a string.

Note

The resulting pattern cannot have a repeating quantifier applied to it.

class pregex.core.assertions.MatchAtEnd(pre: Union[Pregex, str])[source]

Matches the provided pattern only if it is at the end of the string.

Parameters

pre (Pregex | str) – The pattern that is to be matched.

Raises

InvalidArgumentTypeException – Parameter pre is neither a Pregex instance nor a string.

Note

The resulting pattern cannot have a repeating quantifier applied to it.

class pregex.core.assertions.MatchAtLineEnd(pre: Union[Pregex, str])[source]

Matches the provided pattern only if it is at the end of a line.

Parameters

pre (Pregex | str) – The pattern that is to be matched.

Raises

InvalidArgumentTypeException – Parameter pre is neither a Pregex instance nor a string.

Note
  • The resulting pattern cannot have a repeating quantifier applied to it.

  • Uses meta character $ since the MULTILINE flag is considered on.

class pregex.core.assertions.MatchAtLineStart(pre: Union[Pregex, str])[source]

Matches the provided pattern only if it is at the start of a line.

Parameters

pre (Pregex | str) – The pattern that is to be matched.

Raises

InvalidArgumentTypeException – Parameter pre is neither a Pregex instance nor a string.

Note
  • The resulting pattern cannot have a repeating quantifier applied to it.

  • Uses meta character ^ since the MULTILINE flag is considered on.

class pregex.core.assertions.MatchAtStart(pre: Union[Pregex, str])[source]

Matches the provided pattern only if it is at the start of the string.

Parameters

pre (Pregex | str) – The pattern that is to be matched.

Raises

InvalidArgumentTypeException – Parameter pre is neither a Pregex instance nor a string.

Note

The resulting pattern cannot have a repeating quantifier applied to it.

class pregex.core.assertions.NonWordBoundary[source]

Asserts that the position, at which an instance of this class is placed, must not constitute a word boundary.

class pregex.core.assertions.NotEnclosedBy(match: Union[Pregex, str], *assertions: Union[Pregex, str])[source]

Matches pattern match only if it is neither directly preceded nor followed by any one of the provided assertions patterns.

Parameters
  • match (Pregex | str) – The pattern that is to be matched.

  • *assertions (Pregex | str) – One or more patterns, none of which must come either right before or right after pattern match in order for it to be considered a match.

Raises
  • NotEnoughArgumentsException – No assertion patterns were provided.

  • InvalidArgumentTypeException – At least one of the provided arguments is neither a Pregex instance nor a string.

  • EmptyNegativeAssertionException – At least one of the provided assertion patterns is the empty-string pattern.

  • NonFixedWidthPatternException – At least one of the provided assertion patterns does not have a fixed width.

class pregex.core.assertions.NotFollowedBy(match: Union[Pregex, str], *assertions: Union[Pregex, str])[source]

Matches pattern match only if it is not directly followed by any one of the provided assertions patterns.

Parameters
  • match (Pregex | str) – The pattern that is to be matched.

  • *assertions (Pregex | str) – One or more patterns, none of which must come right after pattern match in order for it to be considered a match.

Raises
  • NotEnoughArgumentsException – No assertion patterns were provided.

  • InvalidArgumentTypeException – At least one of the provided arguments is neither a Pregex instance nor a string.

  • EmptyNegativeAssertionException – At least one of the provided assertion patterns is the empty-string pattern.

class pregex.core.assertions.NotPrecededBy(match: Union[Pregex, str], *assertions: Union[Pregex, str])[source]

Matches pattern match only if it is not directly preceded by any one of the provided assertions patterns.

Parameters
  • match (Pregex | str) – The pattern that is to be matched.

  • *assertions (Pregex | str) – One or more patterns, none of which must come right before pattern match in order for it to be considered a match.

Raises
  • NotEnoughArgumentsException – No assertion patterns were provided.

  • InvalidArgumentTypeException – At least one of the provided arguments is neither a Pregex instance nor a string.

  • EmptyNegativeAssertionException – At least one of the provided assertion patterns is the empty-string pattern.

  • NonFixedWidthPatternException – At least one of the provided assertion patterns does not have a fixed width.

class pregex.core.assertions.PrecededBy(match: Union[Pregex, str], *assertions: Union[Pregex, str])[source]

Matches pattern match only if it is directly preceded by all of the provided assertion patterns.

Parameters
  • match (Pregex | str) – A Pregex instance or string representing the match pattern.

  • *assertions (Pregex | str) – One or more patterns, all of which must come right before pattern match in order for it to be considered a match.

Raises
  • NotEnoughArgumentsException – No assertion patterns were provided.

  • InvalidArgumentTypeException – At least one of the provided arguments is neither a Pregex instance nor a string.

  • NonFixedWidthPatternException – Parameter assertion corresponds to a pattern that does not have a fixed width.

Note

The resulting pattern cannot have a repeating quantifier applied to it.

class pregex.core.assertions.WordBoundary[source]

Asserts that the position, at which an instance of this class is placed, must constitute a word boundary.