Skip to content

Instantly share code, notes, and snippets.

@mcm
Created February 1, 2021 17:18
Show Gist options
  • Save mcm/91a610ca0461eefac94ce8d904312415 to your computer and use it in GitHub Desktop.
Save mcm/91a610ca0461eefac94ce8d904312415 to your computer and use it in GitHub Desktop.
def extract_regex(input_text=None, regex=None, **kwargs):
"""
Args:
input_text
regex
Returns a JSON-serializable object that implements the configured data paths:
groups
groupdict.*
"""
############################ Custom Code Goes Below This Line #################################
import json
import re
import phantom.rules as phantom
outputs = {
"groups": [],
"groupdict": {}
}
regex = re.compile(regex)
m = regex.search(input_text)
if m:
outputs["groups"] = m.groups()
outputs["groupdict"] = m.groupdict()
# Return a JSON-serializable object
assert json.dumps(outputs) # Will raise an exception if the :outputs: object is not JSON-serializable
return outputs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment