Created
May 21, 2015 11:23
-
-
Save hiteshd/1482fe9ea22753f99744 to your computer and use it in GitHub Desktop.
Program to print the characters given a very simple regular expression. Musings of http://stackoverflow.com/questions/30371433/how-to-get-the-corresponding-chars-with-a-pattern-in-python?noredirect=1#comment48833548_30371433
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
#!/usr/bin/env python | |
import sys | |
import re | |
master_pattern = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-" | |
if len(sys.argv) != 2: | |
print "Usage: ./%s [pattern]\n" % sys.argv[0] | |
sys.exit(1) | |
input_pattern = sys.argv[1] | |
matches = re.match("\[([a-zA-Z0-9_-])\-([a-zA-Z0-9_-])\]", input_pattern) | |
if matches: | |
first_element = matches.group(1) | |
last_element = matches.group(2) | |
first_element_idx = master_pattern.index(first_element) | |
last_element_idx = master_pattern.index(last_element) | |
for index in range(first_element_idx, last_element_idx + 1): | |
print master_pattern[index] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: This gist has some bugs in parse the bracket pattern. The recommended way is proposed by swenzel. People can check it out here
---------------------------------------DEPRECATED----------------------------------------------------------
This is all the available test cases:
--------------------------------------DEPRECATED--------------------------------------------------