matx.runtime.regex module

class matx.runtime.regex.Regex(pattern, ignore_case=False, dotall=False, extended=False, anchored=False, ucp=True)[source]

Bases: Object

Regular class implemented using pcre.

Parameters:
  • pattern (str) – Str types. Regular expression pattern.

  • ignore_case (bool) – Booleans. Perform case-insensitive matching. The default is false

  • dotall (bool) – Booleans. “.” matches any character at all, including the newline. The default is false

  • extended (bool) – Booleans. Most white space in the pattern (other than in a character class), and characters between a # outside a character class and the next newline, inclusive, are ignored. An escaping backslash can be used to include a white space or # character as part of the pattern. The default is false.

  • anchored (bool) – Booleans. Matches only at the beginning of the subject. The default is false.

  • ucp (bool) – Booleans. Sequences such as “d” and “w” use Unicode properties to determine character types, instead of recognizing only characters with codes less than 128 via a lookup table. The default is false.

Examples

>>> import matx
>>> regex = matx.Regex("(?<first>.*) are (?<second>.*?) .*")
>>> regex
Object(0x55c11322a200)
__init__(pattern, ignore_case=False, dotall=False, extended=False, anchored=False, ucp=True)[source]
match(string: Union[str, bytes], offset: int = 0)[source]

Try to apply the pattern at the start of the string, returning a tuple containing the matched string. If grouping version of regular pattern is used, then the text of all groups are returned.

Parameters:
  • string (str|bytes) – The source string.

  • offset (int) – Offset in the subject at which to start matching

Returns:

The matched groups. The first element in the tuple is indexed groups. The second element in the tuple is named groups.

Return type:

Tuple(List, Dict)

Examples

>>> import matx
>>> regex = matx.Regex("(?<first>.*) are (?<second>.*?) .*")
>>> matched_result = regex.match("Cats are smarter than dogs")
>>> matched_result[0]
['Cats are smarter than dogs', 'Cats', 'smarter']
>>> matched_result[1]
{'first': 'Cats', 'second': 'smarter'}
replace(string: Union[str, bytes], repl: Union[str, bytes])[source]

Return the string obtained by replacing the leftmost non-overlapping occurrences of the pattern in the input string by the replacement repl.

Parameters:
  • string (str|bytes) – The source string.

  • repl (str|bytes) – The replacement string.

Returns:

The replaced string. If no match was found, returning the source string.

Return type:

str|bytes

Examples

>>> import matx
>>> regex = matx.Regex("name")
>>> new_str = regex.replace("mynameisHE", "NAME")
>>> new_str
myNAMEisHE
split(string: Union[str, bytes])[source]

Split a string by the occurrences of a pattern.

Parameters:

string (str|bytes) – The source string.

Returns:

A list containing the resulting substrings. If no match was found,returning a list containing only the source string, i.e. [input].

Return type:

List[str|bytes]

Examples

>>> import matx
>>> regex = matx.Regex("name")
>>> tokens = regex.split("mynameisHE")
>>> tokens
['my', 'isHE']