Skip to content

Instantly share code, notes, and snippets.

@tapanhp
Created February 20, 2019 07:19
Show Gist options
  • Save tapanhp/1ca9380c52280184ec97879f93fd2c98 to your computer and use it in GitHub Desktop.
Save tapanhp/1ca9380c52280184ec97879f93fd2c98 to your computer and use it in GitHub Desktop.
issue_regex
import re
def test_patterns(text, patterns):
"""Given source text and a list of patterns, look for
matches for each pattern within the text and print
them to stdout.
"""
# Look for each pattern in the text and print the results
for pattern, desc in patterns:
print("'{}' ({})\n".format(pattern, desc))
print(" '{}'".format(text))
for match in re.finditer(pattern, text):
s = match.start()
e = match.end()
substr = text[s:e]
n_backslashes = text[:s].count('\\')
prefix = '.' * (s + n_backslashes)
print(" {}'{}'".format(prefix, substr))
print()
return
if __name__ == '__main__':
test_patterns('abbaaabbbbaaaaa',
[('ab', "'a' followed by 'b'"),
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment