drugforge.data.util.utils.construct_regex_function
- drugforge.data.util.utils.construct_regex_function(pat, fail_val=None, ret_groups=False)[source]
Construct a function that searches for the given regex pattern, either returning fail_val or raising an error if no match is found. The output of the returned function will depend on the value passed for
ret_groups. IfTrue, then both the overall match and the tuple of captured groups will be returned. IfFalse, only one value will be returned. If there is a capture group inpat, we assume that’s what should be matched and will return the first captured group. Otherwise, the full match will be returned.- Parameters:
pat (str) – Regular expression to search for
fail_val (str, optional) – If a value is passed, this value will be returned from the re searches if a match isn’t found. If None (default), a ValueError will be raised from the re search
ret_groups (bool, default=False) – If True, return the whole match, as well as any groups that were captured
- Returns:
regex_func – Fucntion that searches for pattern in pat
- Return type:
Callable