Skip to content

Instantly share code, notes, and snippets.

@draperjames
Last active May 22, 2017 14:02
Show Gist options
  • Save draperjames/6c82df4c0b351511fb518d92239c8159 to your computer and use it in GitHub Desktop.
Save draperjames/6c82df4c0b351511fb518d92239c8159 to your computer and use it in GitHub Desktop.
lil python function for "OR"ed regex terms from a given list.
def multiRegExOr(term_list):
"""Returns a singles string of "OR"ed regex terms from a given list.
Parameters
----------
term_list: list
List of regular expressions.
Returns
-------
all_exp: str
A string pattern ready for use with re methods.
Exaples
-------
>>>my_regex_dict = {"string":"[Ss]trin[gg]", "crazy":"[Cc]ra[Zz]*y]"}
>>>re.findall(multiRegExOr(my_regex_dict.values()),
>>> 'My CraZzy StrinG')
See Also
--------
re.search
re.match
"""
all_exp = "|".join(["(%s)" % (term) for term in term_list])
return all_exp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment