matx.runtime.regex module

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

基类:Object

Regular class implemented using pcre.

参数:
  • 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.

示例

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

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.

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

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

返回:

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

返回类型:

Tuple(List, Dict)

示例

>>> 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])[源代码]

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

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

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

返回:

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

返回类型:

str|bytes

示例

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

Split a string by the occurrences of a pattern.

参数:

string (str|bytes) – The source string.

返回:

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

返回类型:

List[str|bytes]

示例

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