refex.match

The common match classes, used for representing fragments for substitution.

This is the bare minimum of match information, common to all search modes. refex.python.matcher has matchers that carry information about the AST, which can be used for more sophisticated search/replace functionality.

All match classes have the following two attributes:

string: Optional[str]

If this is non-None, this is the value that was matched as a string. This value is used when the match is used as a source for a substitution.

span: Optional[Tuple[int, int]]

If this is non-None, then this is the location of the match in the source file. This value is used when the match is used as a _destination_ for a substitution.

The span is represented as a tuple of (start, end), which is a half-open range using unicode offsets.

Every match with a span must have a string. (If nothing else, the string can be the contents at that span location.)

Note that the string may be different than the actual textual content at the span destination. For example, consider the Python expression (b + c) * d. If we have a match for the addition operation, it might have a string of "b + c", but a span that is "(b + c)". This is a useful thing to do:

  1. If we replace this expression with "e", it would be nice for the expression to become e * d, rather than (e) * d.

  2. If we substitute this match into a function call, it would be nice for that call to become foo(b + c) rather than foo((b + c)).

class Match

Bases: object

A match with no accompanying information.

string
span
class StringMatch(string: str)

Bases: refex.match.Match

A match which can be a source for substitution.

string
span
class SpanMatch(string: str, span: Tuple[int, int])

Bases: refex.match.StringMatch

A match which can be both a source and destination for substitution.

string
span
classmethod from_text(text: str, span: Tuple[int, int]) → refex.match.SpanMatch

Creates a SpanMatch from a span within text.

class ObjectMatch(matched: Any)

Bases: refex.match.Match

Match that carries data with it, but has no associated span or string.

string
span
matched = None

An object associated with the match.