Module-level functions
These module-level entry points mirror re and are thin adapters
around the Rust extension. Each accepts either a str or
pathlib.Path as the file path and dispatches on
max_span_lines.
- class file_re.core.file_re_cls[source]
Static facade that exposes the public
file_reAPI.The class mirrors the module-level surface of
re. Each method is a thin adapter over the Rust extension: it coercesPathinputs, validatesmax_span_lines, emits warnings for flags that diverge fromre, and wraps results in PythonMatchobjects.- static search(pattern, file_path, flags=0, *, max_span_lines=None)[source]
Scan the file and return the first match anywhere.
- Parameters:
pattern (str) – The regex pattern to search for.
file_path (str or pathlib.Path) – Path to the file.
.gzand.xzarchives are decoded transparently.flags (int, optional) – Bitwise OR of
reflags. Defaults to0.max_span_lines (int or None, optional) – Controls how much of the file is held in memory.
None(default) loads the whole file;1scans line by line;N > 1uses a slidingN-line window.
- Returns:
The first match, or
Noneif no match is found.- Return type:
Match or None
- Raises:
ValueError – If
max_span_linesis provided and not>= 1, ifpatternis invalid, or ifflagscontainsre.LOCALE.OSError – If the file cannot be opened or read.
Examples
>>> from file_re import file_re >>> m = file_re.search(r"ERROR: (\w+)", "server.log") >>> m.group(1) if m else None
- static match(pattern, file_path, flags=0, *, max_span_lines=None)[source]
Match the pattern anchored at the start of the file.
- Parameters:
- Returns:
The match, or
Noneif the pattern does not match at position 0.- Return type:
Match or None
- Raises:
ValueError – If
max_span_linesis not>= 1, ifpatternis invalid, or ifflagscontainsre.LOCALE.OSError – If the file cannot be opened or read.
- static findall(pattern, file_path, flags=0, *, max_span_lines=None)[source]
Return all non-overlapping matches in the file.
- Parameters:
- Returns:
If
patternhas no capturing groups, a list of match strings. Otherwise, a list of tuples containing the captured groups (non-participating groups areNone).- Return type:
- Raises:
ValueError – If
max_span_linesis not>= 1, ifpatternis invalid, or ifflagscontainsre.LOCALE.OSError – If the file cannot be opened or read.
- static finditer(pattern, file_path, flags=0, *, max_span_lines=None)[source]
Iterate lazily over all non-overlapping matches in the file.
- Parameters:
- Returns:
Yields
Matchobjects lazily. The underlying file is closed when the iterator is exhausted or garbage collected.- Return type:
Iterator of Match
- Raises:
ValueError – If
max_span_linesis not>= 1, ifpatternis invalid, or ifflagscontainsre.LOCALE.OSError – If the file cannot be opened.
Examples
>>> from file_re import file_re >>> for m in file_re.finditer(r"\bERROR\b", "server.log", max_span_lines=1): ... print(m.span(), m.group())