Pattern
- class file_re.Pattern(pattern, flags=0)[source]
Bases:
objectA compiled regex pattern, mirroring
re.Pattern.Instances are produced by
file_re.compile()and hold a compiled Rust regex. The compiled pattern is reused across calls, avoiding recompilation overhead for patterns applied to multiple files.- Parameters:
- Raises:
ValueError – If
patternis not a valid regex, or ifflagscontainsre.LOCALE.
- property pattern: str
The regex source string used to compile this pattern.
- Returns:
The original pattern string.
- Return type:
- search(file_path, max_span_lines=None)[source]
Scan the file and return the first match anywhere.
- Parameters:
file_path (str or pathlib.Path) – Path to the target file. Gzip (
.gz) and xz (.xz) archives are decoded transparently.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.OSError – If the file cannot be opened or read.
- match(file_path, max_span_lines=None)[source]
Match the pattern anchored at the start of the file.
- Parameters:
file_path (str or pathlib.Path) – Path to the target file.
- Returns:
The match, or
Noneif the pattern does not match at position 0.- Return type:
Match or None
- Raises:
ValueError – If
max_span_linesis provided and not>= 1.OSError – If the file cannot be opened or read.
- findall(file_path, max_span_lines=None)[source]
Return all non-overlapping matches in the file.
- Parameters:
file_path (str or pathlib.Path) – Path to the target file.
- Returns:
If the pattern has no capturing groups, a list of match strings. Otherwise, a list of tuples containing the captured groups (non-participating groups are
None).- Return type:
- Raises:
ValueError – If
max_span_linesis provided and not>= 1.OSError – If the file cannot be opened or read.
- finditer(file_path, max_span_lines=None)[source]
Iterate lazily over all non-overlapping matches in the file.
- Parameters:
file_path (str or pathlib.Path) – Path to the target file.
- Returns:
A lazy iterator that yields
Matchobjects. The underlying file is closed when the iterator is exhausted or garbage collected.- Return type:
Iterator of Match
- Raises:
ValueError – If
max_span_linesis provided and not>= 1.OSError – If the file cannot be opened.