Created
February 20, 2019 07:19
-
-
Save tapanhp/1ca9380c52280184ec97879f93fd2c98 to your computer and use it in GitHub Desktop.
issue_regex
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
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