Created
November 6, 2015 18:01
-
-
Save ptrourke/764588ef4bbd06a7641c to your computer and use it in GitHub Desktop.
Simple script for running tests of regular expression patterns against a list of input strings.
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
__author__ = 'ptrourke' | |
import re | |
''' | |
A simple script for running tests of regular expressions (regexes) against a list of input strings. | |
To use, change the values of test_strings, test_patterns, and group_indices . | |
''' | |
# The strings to be used as test vectors to test the regexes | |
test_strings = [ | |
'march_of_the_penguins', | |
'abcdefg_hijklmnop_QRSTUVW_xyz_ABC', | |
"this-shouldn't=match-at-all" | |
] | |
# The regex patterns to test against the test vectors | |
test_patterns = [ | |
'^(.*)_(.*)_.*$', | |
'^(.*?)_(.*)_.*$', | |
'^(.*)_(.*?)_.*$', | |
'^(.*?)_(.*?)_.*$', | |
'^([^_]*)_([^_]*)_.*$', | |
] | |
# The match groups to display (use `[0]` to show the full string). | |
group_indices = [1,2] | |
def test_regex(regex, item, group_indices): | |
m = re.match(regex, item) | |
result_list = [] | |
for index in group_indices: | |
result_list.append(m.group(index)) | |
return str(result_list) | |
def test_regexes(test_strings, test_patterns, group_indices): | |
test_string_count = len(test_strings) | |
pattern_count = len(test_patterns) | |
group_count = len(group_indices) | |
print 'Testing %d test strings against %d regexes\n' % (test_string_count, pattern_count ) | |
pattern_length = len(max(test_patterns, key=len)) + 4 | |
for item in test_strings: | |
print "Test string: %s\n" % item | |
item_length = group_count * (len(item) + 5) | |
group_listing_string = ', '.join(['Group %d' % group_id for group_id in group_indices]) | |
print '%s %s %s' % ('# '.rjust(3), 'Regex '.ljust(pattern_length), ('Matches: %s' % group_listing_string ).ljust(item_length)) | |
for index, test_pattern in list(enumerate(test_patterns)): | |
try: | |
regex_test_result = test_regex(test_pattern, item, group_indices) | |
except AttributeError, e: | |
regex_test_result = '' | |
print '%s %s: %s' % (('%d.' % (index + 1)).rjust(3), ('\"%s\"' % test_pattern).ljust(pattern_length), regex_test_result.ljust(item_length)) | |
print " \n" | |
test_regexes(test_strings, test_patterns, group_indices) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output: