delitepy.ne_re package

Submodules

delitepy.ne_re.match module

class delitepy.ne_re.match.Match

Bases: object

end(index: int = 0) int

Return the indices of the end of the substring matched by group; group defaults to zero (meaning the whole matched substring). Return -1 if group exists but did not contribute to the match.

Parameters

indexstr

group index for which ending position in input string is required

Returns

ending position in input string

group(*args: int) str | tuple[str]

Returns one or more subgroups of the match. If there is a single argument, the result is a single string; if there are multiple arguments, the result is a tuple with one item per argument. Without arguments, group1 defaults to zero (the whole match is returned). If a groupN argument is zero, the corresponding return value is the entire matching string; If a group number is negative or larger than the number of groups defined in the pattern, an exception is raised. If a group is contained in a part of the pattern that did not match, the corresponding result is None. If a group is contained in a part of the pattern that matched multiple times, the last match is returned.

Examples

>>> m = ne_re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
>>> m.group(0)       # 'Isaac Newton'
>>> m.group(1)       # 'Isaac'
>>> m.group(2)       # 'Newton'
>>> m.group(1, 2)    # ('Isaac', 'Newton')

Note: Named groups are not supported like regex pattern containing (?P<name>…) syntax.

If a group matches multiple times, only the last match is accessible:

Examples

>>> m = ne_re.match(r"(..)+", "a1b2c3")  # Matches 3 times.
>>> m.group(1)                           # 'c3'

Parameters

args*int

zero or more indices of groups

Returns

string or tuple of strings conatining matched groups

groups(default: str = None) tuple[str | None]

Return a tuple containing all the subgroups of the match, from 1 up to however many groups are in the pattern. The default argument is used for groups that did not participate in the match; it defaults to None.

Examples

>>> m = ne_re.match(r"(\d+)\.(\d+)", "24.1632")
>>> m.groups()                  # ('24', '1632')
>>> m = ne_re.match(r"(\d+)\.?(\d+)?", "24")
>>> m.groups()                  # ('24', None)
>>> m.groups('0')               # ('24', '0')

Parameters

defaultstr

Default string to return if matched groups is None

Returns

Tuple of strings or None for matched groups from index 1

span(index: int = 0) tuple[int, int]

Return the tuple of start and end indices of the substring matched by group; group defaults to zero (meaning the whole matched substring). Return (-1, -1) if group exists but did not contribute to the match.

Parameters

indexstr

group index for which starting and ending position in input string is required

Returns

tuple with starting and ending position

start(index: int = 0) int

Return the indices of the start of the substring matched by group; group defaults to zero (meaning the whole matched substring). Return -1 if group exists but did not contribute to the match.

Parameters

indexstr

group index for which starting position in input string is required

Returns

starting position in input string

delitepy.ne_re.regex module

delitepy.ne_re.regex.findall(pattern: str, string: str) list[str] | list[tuple[str]]

Return all non-overlapping matches of pattern in string, as a list of strings or tuples. The string is scanned left-to-right, and matches are returned in the order found. Empty matches are included in the result.

The result depends on the number of capturing groups in the pattern. If there are no groups, return a list of strings matching the whole pattern. If there is exactly one group, return a list of strings matching that group. If multiple groups are present, return a list of tuples of strings matching the groups. Non-capturing groups do not affect the form of the result.

Examples

>>> ne_re.findall(r'f[a-z]*', 'which foot or hand fell fastest') # ['foot', 'fell', 'fastest']
>>> ne_re.findall(r'(\w+)=(\d+)', 'set width=20 and height=10') # [('width', '20'), ('height', '10')]

Parameters

pattern: str

regex pattern to be used for searching

string: str

input string on which to search

Returns

List of strings or a list of tuple of strings

delitepy.ne_re.regex.finditer(pattern: str, string: str) list[Match]

Return a list of Match objects over all non-overlapping matches for the regex pattern in string. The string is scanned left-to-right, and matches are returned in the order found. Empty matches are included in the result.

Parameters

pattern: str

regex pattern to be used for searching

string: str

input string on which to search

Returns

List of match objects

delitepy.ne_re.regex.fullmatch(pattern: str, string: str) Match | None

If the whole string matches the regular expression pattern, return a corresponding Match. Return None if the string does not match the pattern.

Examples

>>> ne_re.fullmatch("p.*n", "python") # <ne_re.Match object; span=(0, 6), match='python'>
>>> ne_re.fullmatch("r.*n", "python") # None

Parameters

pattern: str

regex pattern to be used for searching

string: str

input string on which to search

Returns

Match if pattern found else None.

delitepy.ne_re.regex.match(pattern: str, string: str) Match | None

If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding Match. Return None if the string does not match the pattern.

Examples

>>> m = ne_re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
>>> m.group(0)       # 'Isaac Newton'
>>> m.group(1)       # 'Isaac'
>>> m.group(2)       # 'Newton'
>>> m.group(1, 2)    # ('Isaac', 'Newton')

Parameters

pattern: str

regex pattern to be used for searching

string: str

input string on which to search

Returns

Match if pattern found else None.

delitepy.ne_re.regex.search(pattern: str, string: str) Match | None

Scan through string looking for the first location where the regular expression pattern produces a match, and return a corresponding Match. Return None if no position in the string matches the pattern.

Parameters

pattern: str

regex pattern to be used for searching

string: str

input string on which to search

Returns

Match if pattern found else None.

delitepy.ne_re.regex.split(pattern: str, string: str, return_matched_groups: bool = False) list[str]

Split string by the occurrences of pattern. If capturing parentheses are used in pattern, then the text of all groups in the pattern are also returned as part of the resulting list if return_matched_groups is set to True.

Examples

>>> ne_re.split(r'\W+', 'Words, words, words.') # ['Words', 'words', 'words']
>>> ne_re.split(r'(\W+)', 'Words, words, words.', True) # ['Words', ', ', 'words', ', ', 'words', '.']
>>> ne_re.split(r'(\W+)', 'Words, words, words.', False) # ['Words', 'words', 'words']
>>> ne_re.split('[a-f]+', '0a3b9') # ['0', '3', '9']

Parameters

pattern: str

regex pattern to be used for searching

string: str

input string on which to search

return_matched_groups: bool

Whether matched groups should also be returned in the output

Returns

splits:

List of strings after splitting the original string

delitepy.ne_re.regex.sub(pattern: str, replacement: str, string: str, count: int = 0) str

Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement. If the pattern isn’t found, string is returned unchanged.

Examples

>>> ne_re.sub(r'\sAnd\s', ' & ', 'Baked Beans And Spam') # 'Baked Beans & Spam'

Parameters

pattern: str

regex pattern to be used for searching

replacement: str

replacement string to be used when a pattern is found

string: str

input string on which to search

count: int

maximum number of replacements to be done

Returns

Modified string after substituting replacement in string for pattern

delitepy.ne_re.regex.subn(pattern: str, replacement: str, string: str, count: int = 0) tuple[str, int]

Perform the same operation as sub(), but return a tuple (new_string, number_of_subs_made).

Parameters

pattern: str

regex pattern to be used for searching

replacement: str

replacement string to be used when a pattern is found

string: str

input string on which to search

count: int

maximum number of replacements to be done

Returns

Tuple with modified string after substituting replacement in string for pattern and count of substitutions applied.

Module contents

Package delitepy.ne_re.