find_matched_str

pyhelpers.text.find_matched_str(input_str, lookup_list, use_regex=True)[source]

Finds all strings (in a sequence) that match a given string or regex pattern.

Parameters:
  • input_str (str) – The string to match.

  • lookup_list (Iterable[str]) – A sequence of strings for lookup.

  • use_regex (bool) – Whether to treat text as a regex pattern; defaults to True.

Returns:

A generator containing all strings that match text.

Return type:

Generator | None

Examples:

>>> from pyhelpers.text import find_matched_str
>>> lookup_lst = ['abc', 'aapl', 'app', 'ap', 'ape', 'apex', 'apel']
>>> text_ = find_matched_str('apple', lookup_lst)
>>> list(text_)
[]
>>> lookup_lst += ['apple']
>>> lookup_lst
['abc', 'aapl', 'app', 'ap', 'ape', 'apex', 'apel', 'apple']
>>> text_ = find_matched_str('apple', lookup_lst)
>>> list(text_)
['apple']
>>> text_ = find_matched_str(r'app(le)?', lookup_lst)
>>> list(text_)
['app', 'apple']
>>> text_ = find_matched_str('app(le)?', lookup_lst, use_regex=False)
>>> list(text_)  # No match because regex behavior is disabled
[]