pysys.utils.filegrep¶
Regular expression grep matching in text files.
getmatches¶
- pysys.utils.filegrep.getmatches(file, regexpr, ignores=None, encoding=None, encodingReplaceOnError=False, flags=0, mappers=[], returnFirstOnly=False)[source]¶
Look for matches on a regular expression in an input file, return a sequence of the matches (or if returnFirstOnly=True, just the first).
- Parameters
file – The full path to the input file
regexpr – The regular expression used to search for matches
mappers – A list of lambdas or generator functions used to pre-process the file’s lines before looking for matches.
ignores – A list of regexes which will cause matches to be discarded. These are applied after any mappers.
encoding – Specifies the encoding to be used for opening the file, or None for default.
encodingReplaceOnError (bool) – Set to True to replace erroneous characters that are invalid in the expected encoding (with a backslash escape) rather than throwing an exception.
returnFirstOnly – If True, stops reading the file as soon as the first match is found and returns it.
- Returns
A list of the match objects, or the match object or None if returnFirstOnly is True
- Return type
list
- Raises
FileNotFoundException – Raised if the input file does not exist
filegrep¶
- pysys.utils.filegrep.filegrep(file, expr, returnMatch=False, **kwargs)[source]¶
Search for matches to a regular expression in an input file, returning true if a match occurs.
- Parameters
file – The full path to the input file
expr – The regular expression (uncompiled) to search for in the input file
ignores – Optional list of regular expression strings to ignore when searching file.
returnMatch – return the regex match object instead of a simple boolean
encoding – Specifies the encoding to be used for opening the file, or None for default.
mappers – Mappers to pre-process the file.
- Returns
success (True / False), unless returnMatch=True in which case it returns the regex match object (or None if not matched)
- Return type
integer
- Raises
FileNotFoundException – Raised if the input file does not exist
lastgrep¶
- pysys.utils.filegrep.lastgrep(file, expr, ignore=[], include=[], encoding=None, returnMatch=False, flags=0)[source]¶
Search for matches to a regular expression in the last line of an input file, returning true if a match occurs.
- Parameters
file – The full path to the input file
expr – The regular expression (uncompiled) to search for in the last line of the input file
ignore – A list of regular expressions which remove entries in the input file contents before making the grep
include – A list of regular expressions used to select lines from the input file contents to use in the grep
encoding – Specifies the encoding to be used for opening the file, or None for default.
- Returns
success (True / False)
- Return type
integer
- Raises
FileNotFoundException – Raised if the input file does not exist
orderedgrep¶
- pysys.utils.filegrep.orderedgrep(file, exprList, encoding=None, flags=0)[source]¶
Seach for ordered matches to a set of regular expressions in an input file, returning None on success, and a string indicating the missing expression something is missing.
The ordered grep method will only pass if matches to the set of regular expression in the expression list occur in the input file in the order they appear in the expression list. Matches to the regular expressions do not have to be across sequential lines in the input file, only in the correct order. For example, for a file with contents
A is for apple B is for book C is for cat D is for dog
an expression list of [“^A.*$”, “^C.*$”, “^D.*$”] will return true, whilst an expression list of [“^A.*$”, “^C.$”, “^B.$”] will return false.
- Parameters
file – The full path to the input file
exprList – A list of regular expressions (uncompiled) to search for in the input file
encoding – Specifies the encoding to be used for opening the file, or None for default.
- Returns
None on success, or on failure the string expression that was not found (with an indicator of its index in the array).
- Return type
string
- Raises
FileNotFoundException – Raised if the input file does not exist