Last active
May 22, 2017 14:02
-
-
Save draperjames/6c82df4c0b351511fb518d92239c8159 to your computer and use it in GitHub Desktop.
lil python function for "OR"ed regex terms from a given list.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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