Skip to content

Instantly share code, notes, and snippets.

@ph87
Last active May 19, 2017 03:16
Show Gist options
  • Save ph87/fe1af93cee793d3b7ffe3a0f81b13f96 to your computer and use it in GitHub Desktop.
Save ph87/fe1af93cee793d3b7ffe3a0f81b13f96 to your computer and use it in GitHub Desktop.
def grep(pattern_str):
''' Using yield receive data inside generator
:param pattern_str: rex string
Demo::
>>> finder = grep('target \d+')
>>> next(finder)
>>> print(finder.send('target 1234'))
target 1234
>>> print(finder.send('salt 1234'))
None
>>> print(finder.send('salt target 1234'))
salt target 1234
'''
sre = re.compile(pattern_str)
while True:
message = yield
sre_search = sre.search(message)
yield sre_search and message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment