diff --git a/one_line_regex.py b/one_line_regex.py index 19b679e..ecf6ddc 100644 --- a/one_line_regex.py +++ b/one_line_regex.py @@ -5,20 +5,21 @@ import sys -def magic_match(pattern, target): +def magic_match(*args, **kwargs): """ Match a regex against a target string. - Assign the result to a 'match' variable in the *caller's scope*. + Assign the result to a 'match' variable in the *caller's global scope*. """ frame = sys._getframe(1) - result = re.match(pattern, target) + result = re.match(*args, **kwargs) # This is properly, properly evil. Don't do this: - frame.f_locals['match'] = result + frame.f_globals['match'] = result return result -if magic_match(r'[abcde]+', sys.argv[1]): - print 'Your match was %r' % match.group(0) -else: - print 'There was no match' +if __name__ == '__main__': + if magic_match(r'[abcde]+', sys.argv[1]): + print 'Your match was %r' % match.group(0) + else: + print 'There was no match'